加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 16:42:15 所属栏目:资源 来源:网络整理
导读:这是我的模型: public class Items { public string Foo { get; set; } public string Bar { get; set; } } 控制器: public ActionResult Index() { var model = new ListItems { new Items { Foo = "foo",Bar = "bar" },new Items { Foo = "ai",Bar = "ia
这是我的模型:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读