asp.net mvc 4 - MVC 4 Binding to a dictionary -
i have looked around on internet, none of solutions seem work me.
i have following view model:
public class configsviewmodel { // ios dictionary public dictionary<string, object> iosdictionary { get; set; } // android dictionary public dictionary<string, object> androiddictionary { get; set; } }
the values in these dictionaries either bool or strings. want display table editing view, , want bind these dictionaries. view looks this:
@model mobilerebrandingtool.models.viewmodels.configsviewmodel
<div> @using (html.beginform("saveappconfig", "project", "post")) { <table class="config-table"> <tr> <th></th> <th>ios</th> <th>android</th> </tr> @foreach (var kvp in model.iosdictionary) { <tr> <td> @kvp.key </td> <td> @html.editorfor(model => model.iosdictionary[kvp.key]) </td> <td> @if (model.androiddictionary.containskey(kvp.key)) { @html.editorfor(model => model.androiddictionary[kvp.key]) } </td> </tr> } </table> <input type="submit" value="save"/> } </div>
the problem in saveappconfig action, when check model, instead of string value keys in dictionaries, value array of 1 string (the value in text input). in dictionaries have values bools , strings, cannot use textboxfor. there better way bind dictionaries? should write custom binder? , if so, how should like?
thank you,
cosmin
since in foreach need pull value little differently
@foreach(var kvp in model.iosdictionary){ @html.editorfor(model => model.iosdictionary[kvp.key]) }
should like
@foreach(var kvp in model.iosdictionary){ @html.editorfor(model => kvp.value) }
i not 100% sure can kvp.value. looked iterating through dictionary , found what best way iterate on dictionary in c#?
Comments
Post a Comment