I try to bind an OrderedDictionary to a view but when the post method gets invoked the Dictionary is always empty.
Here is my code:
[HttpGet]
public ViewResult Edit(string username, string password)
{
Xml test = new Xml(@"c:\Users\pc\Desktop\xml - Copy.xml");
XmlNode userNode = test.GetUserNodeByUsernameAndPassword(username, password);
User user = new User();
user.BindData(userNode);
return View(user.user);
}
[HttpPost]
public ViewResult Edit(OrderedDictionary attributes)
{
return View(attributes);
}
And here is the view:
@using (Html.BeginForm("Edit", "Users")) {
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<p>
<input type="submit" value="Save" />
</p>
@{int counter = 0;}
@{string name = "";}
@foreach (DictionaryEntry attribute in Model)
{
{ name = "[" + counter + "].key"; }
<input type="hidden" name=@name value=@attribute.Key />
@attribute.Key @Html.TextBoxFor(m => attribute.Value)
counter++;
<br />
}
</fieldset>
}
And the result Html looks like this is:
<input type="hidden" value="Username" name="[0].key">
Username
<input id="attribute_Value" type="text" value="Anamana" name="attribute.Value">
So the content of the OrderedDictionary appears fine in the view but when I make a post back the binding isn’t working and the directory remains empty.
Meantime I have found the solution.
I can pass an
OrderedDictionaryto the view page.It process it by the following
Razorcode:The result
HTML‘s structure fits to the samples which is found in a book, the values from the dictionary appears fine on the page.After POST was invoked the POST handler function gets the modified values in a
Dictionary.I don’t know why but I can’t use
OrderedDictionaryhere.