html – ASP.NET MVC模型列表绑定

前端之家收集整理的这篇文章主要介绍了html – ASP.NET MVC模型列表绑定前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的模型:
public class Items
    {
        public string Foo { get; set; }
        public string Bar { get; set; }
    }

控制器:

public ActionResult Index()
    {
        var model = new List<Items>
                        {
                            new Items
                                {
                                    Foo = "foo",Bar = "bar"
                                },new Items
                                {
                                    Foo = "ai",Bar = "ia"
                                },new Items
                                {
                                    Foo = "one",Bar = "two"
                                }
                        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(List<Items> model)
    {
        return View(model);
    }

查看(索引):

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        <div onclick="$(this).remove();">
            @Html.TextBoxFor(model => model[i].Foo) <br/>
            @Html.TextBoxFor(model => model[i].Bar)
        </div>
    }
    <div>
        <input type="submit"/>
    </div>
}

删除了第二对:

<div onclick="$(this).remove();">
        <input name="[0].Foo" type="text" value="foo"> <br>
        <input name="[0].Bar" type="text" value="bar">
    </div>

    <div onclick="$(this).remove();">
        <input name="[2].Foo" type="text" value="one"> <br>
        <input name="[2].Bar" type="text" value="two">
    </div>

发布时,我只获得第一对(“foo”和“bar”).这是因为第三对的索引为“2”.我想得到两个对(不使用FormCollection.我希望它自动绑定).实际上,我在表单上有很多其他输入,所以我不想重新加载并重新附加索引到每个输入.你能帮助我吗?

解决方法

这可能对你有所帮助….

需要在每个项目上放置隐藏字段…

MVC3 Non-Sequential Indices and DefaultModelBinder

原文链接:https://www.f2er.com/html/227016.html

猜你在找的HTML相关文章