C# 如何绑定OrderedDictionary?

C# 如何绑定OrderedDictionary?,c#,.net,asp.net-mvc,model-binding,C#,.net,Asp.net Mvc,Model Binding,我试图将一个OrderedDictionary绑定到一个视图,但是当调用post方法时,字典总是空的 这是我的密码: [HttpGet] public ViewResult Edit(string username, string password) { Xml test = new Xml(@"c:\Users\pc\Desktop\xml - Copy.xml"); XmlNode userNode = test.GetUserNode

我试图将一个
OrderedDictionary
绑定到一个视图,但是当调用post方法时,字典总是空的

这是我的密码:

    [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);
    }
以下是观点:

@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>
}
使用(Html.BeginForm(“编辑”、“用户”)){ @Html.ValidationSummary(true) 使用者

@{int counter=0;} @{string name=”“;} @foreach(模型中的DictionaryEntry属性) { {name=“[”+计数器+“].key”;} @attribute.Key@Html.TextBoxFor(m=>attribute.Value) 计数器++;
} } 结果Html如下所示:

<input type="hidden" value="Username" name="[0].key">
  Username
  <input id="attribute_Value" type="text" value="Anamana" name="attribute.Value">

用户名
因此,
OrderedDictionary
的内容在视图中看起来不错,但当我发回一篇文章时,绑定不起作用,目录仍然为空。

要绑定字典,必须更改html输入标记中的name属性。大概是这样的:

在控制器中:

[HttpPost]
public ActionResult Edit(IDictionary<string, string> attributes) 
{  
}
它将以asp.NETMVC能够理解并使其工作的格式呈现


有关数据绑定的更多示例,请查看。

同时,我找到了解决方案

我可以将
OrderedDictionary
传递到查看页面。 它通过以下
Razor
code处理它:

    @model System.Collections.Specialized.OrderedDictionary
    (...)
    @{int counter = 0;}
    @{string name = "";}
    @foreach (DictionaryEntry attribute in Model)
    {
        { name = "[" + counter + "].key"; }
        @Html.Hidden(name, attribute.Key)
        {name = "[" + counter + "].value";}
        @attribute.Key @Html.TextBox(name, attribute.Value)
        counter++;
        <br />
    }

我不知道为什么,但我不能在这里使用
OrderedDictionary

从语法上讲,您建议的HTML帮助程序不正确,并且无法生成正确的HTML绑定结构。但是HttpPost处理程序函数的参数列表很有用。感谢使用
Html.HiddenFor()。
@Html.HiddenFor(model => model[i].Key)
@Html.TextBoxFor(model => model[i].Value)
    @model System.Collections.Specialized.OrderedDictionary
    (...)
    @{int counter = 0;}
    @{string name = "";}
    @foreach (DictionaryEntry attribute in Model)
    {
        { name = "[" + counter + "].key"; }
        @Html.Hidden(name, attribute.Key)
        {name = "[" + counter + "].value";}
        @attribute.Key @Html.TextBox(name, attribute.Value)
        counter++;
        <br />
    }
    [HttpPost]
    public ViewResult Edit(Dictionary<string, string> attributes)
    {}