Is it possible to bind such kind of property?
public KeyValuePair<string, string> Stuff { get; set; }
I’ve tried to use following code in the view, but it does not work:
<%=Html.Text("Stuff", Model.Stuff.Value)%>
<%=Html.Hidden("Model.Stuff.Key", Model.Stuff.Key)%>
KeyValuePair<K,V>is a structure, not a class, so each call to yourStuffproperty returns a copy of the originalKeyValuePair. So, when you bind toModel.Stuff.Valueand toModel.Stuff.Key, you are actually working on two different instances ofKeyValuePair<K,V>, none of which is the one from your model. So when they are updated, it doesn’t update the Stuff property in your model… QEDBy the way, the Key and Value properties are read-only, so you can’t modify them : you have to replace the
KeyValuePairinstanceThe following workaround should work :
Model :
View :