My application is MVC 5, trying to develop multiselect checkbox using the following:
public static Dictionary<int, string> AdditionalInfo = new Dictionary<int, string>()
{
{ 1, "Exercise" },
{ 2, "Diet" }
};
public static int MapAdditionalInfo (string value)
{
return (from v in AdditionalInfo
where v.Value == value
select v.Key).FirstOrDefault();
}
public static string MapAdditionalInfo (int? value)
{
return (from v in AdditionalInfo
where v.Key == value
select v.Value).FirstOrDefault();
}
public string AdditionalInfo
{
get { return MapAdditionalInfo(AdditionalInfoID); }
set { AdditionalInfoID = MapAdditionalInfo(value); }
}
In the View:
@foreach (SelectListItem item in (IEnumerable<SelectListItem>)ViewData["Info"] )
{
<label><input value="@item.Value" name="AdditionalInfoID" id="AdditionalInfoID" type='checkbox' checked="@item.Selected" class="checkbox-inline" /><span class='lbl padding-4'>@item.Value </span></label>
}
In the controller:
ViewData["Info"] = MyModel.AdditionalInfo;
I get the following error:
Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.Int32,System.String]' to type 'System.Collections.Generic.IEnumerable`1[System.Web.Mvc.SelectListItem]'.
Would appreciate your suggestions on how to solve this error or if there a example for a better way to develop a checkbox list.
Aucun commentaire:
Enregistrer un commentaire