2

我为键值对输入创建了一个动态表单,一些值将包含逗号:

    using(Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "parameterForm" }))
    {
    <div id="inputBoxesDIV">
         for(int i = 0; i < Model.GetParameters().Count; i++)
           { 
                Html.TextBoxFor(m => m.GetParameters().ElementAt(i).Name, new { name = "name" + i, size = 20 })
                Html.TextBoxFor(m => m.GetParameters().ElementAt(i).Value, new { name = "Value" + i, size = 60 })
        }
    </div>
    }

我尝试使用 FormCollection 来获得这样的配对:

    [HttpPost]
    public ActionResult Index(FormCollection formCollection)
    {
        foreach (var key in formCollection.AllKeys)
        {
            var value = formCollection[key];
        }

        foreach (var key in formCollection.Keys)
        {
            var value = formCollection[key.ToString()];
        }
    //etc...

但是 FormCollection 使用逗号分隔的字符串,所以它不好。

有什么方法我仍然可以使用 FormCollection 或者你知道我该如何解决它吗?

4

2 回答 2

0

我认为您应该能够基于动态生成的模型生成视图,因此您不应有机会对键值对的名称部分进行更改,因此请删除类似的文本框,

using(Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "parameterForm" }))
    {
    <div id="inputBoxesDIV">
         for(int i = 0; i < Model.GetParameters().Count; i++)
           { 
<label>Model.GetParameters().ElementAt(i).Name</label>

                Html.TextBoxFor(m => m.GetParameters().ElementAt(i).Value, new { name =Model.GetParameters().ElementAt(i).Name , size = 60 })
        }
    </div>
    }

因此用户将对值文本框进行更改,因此在他提交您能够使用键名读取所有值之后,

[HttpPost]
    public ActionResult Index(FormCollection formCollection)
    {
        foreach (var key in formCollection.AllKeys)
        {
            var value = formCollection[key];
        }

        foreach (var key in formCollection.Keys)
        {
            var value = formCollection[key.ToString()];
        }
}

如果您想让用户能够修改键值对中的名称和值,那么您应该尝试这样,

     using(Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "parameterForm" }))
        {
        <div id="inputBoxesDIV">
             for(int i = 0; i < Model.GetParameters().Count; i++)
               { 

<input type="text" name="@String.Format("name{0}",i)" value="@Model.GetParameters().ElementAt(i).Name" size="20"/>

                   <input type="text" name="@String.Format("value{0}",i)" value="@Model.GetParameters().ElementAt(i).Value" size="60"/>
            }
        </div>
        }

在您的发布操作中,

[HttpPost]
    public ActionResult Index(FormCollection formCollection)
    {


        for(int i=0;i<formCollection.AllKeys.Length;i++)
        {
            var value = formCollection["value"+i];
            var name=formCollection["name"+i];
        }
    }

希望这可以帮助。

于 2013-10-11T09:49:25.603 回答
0

为什么不使用模型绑定:

public class KeyValue
{
    public string Name { get; set; }
    public string Value { get; set; }
}
public class Test
{
    public IEnumerable<KeyValue> KV { get; set; }
}

并认为:

@using(Html.BeginForm())
{
    for (var kv = 0; kv < Model.KV.Count(); ++kv)
    {
        @Html.TextBox("KV[" + kv + "].Name", Model.KV.ElementAt(kv).Name);
        @:<br />
        @Html.TextBox("KV[" + kv + "].Value", Model.KV.ElementAt(kv).Value);
        @:<br />
    }

    @:<input type="submit" />
}

在控制器中获取您的模型:

    [HttpPost]
    public ActionResult Index(Test model)
    {
        ...
    }
于 2013-10-11T10:38:56.307 回答