0

I am trying to add dictionary values in the View, Following is my Code :-

public class TO
{
     public int EmployeeId { get; set; }
     public int EmployeeName { get; set; }

     public Dictionary<string, double> Dic1 { get; set; }
     public Dictionary<string, double> Dic2 { get; set; }
}

View :-

@model List<Models.TO>


@for (int i = 0; i < @Model.Count; i++)
{
          @Html.HiddenFor(m => m[i].EmployeeId)
          @Html.HiddenFor(m => m[i].EmployeeName)

          ....
          @Html.HiddenFor(m => m[i].Dic1, Value = "") // Psuedo

}

As i have assigned values to EmployeeId & EmployeeName which i get in POST method in the Controller, How to assign values to Dictionary?

tereško
  • 58,060
  • 25
  • 98
  • 150
Anup
  • 9,396
  • 16
  • 74
  • 138

1 Answers1

1

You can't just put the whole dictionary into a hidden field. Dictionary are enumerable, so you have to work out for each key/value.

@for (int i = 0; i < @Model.Count; i++)
{
      @Html.HiddenFor(m => m[i].EmployeeId)
      @Html.HiddenFor(m => m[i].EmployeeName)

      ....
      foreach (string key in Dic1.Keys)
      { 
              @Html.HiddenFor(m => m[i].Dic1[key])
      }

}
NZeta520
  • 114
  • 4