繁体   English   中英

从表格中获取逗号分隔的值?

[英]Get comma separated values from form?

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

    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,还是您知道如何解决?

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

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];
        }
    }

希望这可以帮助。

为什么不使用模型绑定:

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" />
}

并在cotroller中获取您的模型:

    [HttpPost]
    public ActionResult Index(Test model)
    {
        ...
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM