如何为模型制作动作接受列表?(ASP.Net MVC 5)

本文关键字:ASP Net MVC 列表 模型制作 | 更新日期: 2023-09-27 18:05:28

我能够成功地接受数据库中的一个条目。但是,其余的条目不是来自我动态添加的字段。

这是我的Create for Sibling:

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult Create(List<sibling> siblings)
    {
        if (ModelState.IsValid)
        {
            foreach(sibling x in siblings)
            {
                db.siblings.Add(x);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        return View(siblings);

这是我的兄弟姐妹模型:

public partial class sibling
{
    public int sibling_id { get; set; }
    public int child_id { get; set; }
    public Nullable<int> age { get; set; }
    public string gender { get; set; }
    public virtual child child { get; set; }
}

这是我的Create.cshtml:

@model dummyApp.Schema.TestModels.sibling
@{
ViewBag.Title = "Create";
}
@section Scripts{
<script type="text/javascript">
    $(document).ready(function () {
        //var max_fields = 10; //maximum input boxes allowed
        var wrapper = $(".input_fields_wrap"); //Fields wrapper
        var add_button = $(".add_field_button"); //Add button ID
        var x = 1; //initlal text box count
        $(add_button).on("click", function (e) { //on add input button click
            e.preventDefault();
            //if (x < max_fields) { //max input box allowed
                x++; //text box increment
                $(wrapper).append('<div class="form-group">'
                                        <p>Child ID: <input type="number" name="siblings[' + x + '].child_id"/></p>       '
                                        <p>Age: <input type="number" name="siblings[' + x + '].age"/></p> '
                                        <p>Gender: <input type="text" name="siblings[' + x + '].gender"/></p>   '
                                        <a href="#" class="remove_field">Remove</a>     '
                                    </div>'); //add input box
            //}
        });
        $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
            e.preventDefault(); $(this).parent('div').remove(); x--;
        })
    });
</script>
 }
 <h2>Create</h2>
 @using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    <h4>sibling</h4>
    <div class="input_fields_wrap">
        <button class="add_field_button btn btn-info">Add More Fields</button>
        <br><br>
        <div class="form-group">
            <p>Child ID: <input type="text" name="siblings[0].child_id" /></p>
            <p>Age: <input type="text" name="siblings[0].age" /></p>
            <p>Gender: <input type="text" name="siblings[0].gender" /></p>
        </div>
    </div>
    <div class="form-group">
        <br>
        <div class="col-md-offset-0 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
 }
 <div>
@Html.ActionLink("Back to List", "Index")

如何为模型制作动作接受列表?(ASP.Net MVC 5)

你的x的初始值是1,你正在做x++之前传递给动态html,你在javascript中创建;这将渲染元素为siblings[2].child_id。这将破坏索引的连续性,因为缺少可用性[1],并且模型绑定器将在到达序列中的中断时停止绑定。

作为解决方案,

  • 在执行html追加或
  • 后执行x++
  • 使用非顺序索引方法-有一个名称为siblings.Index的隐藏字段,并为该字段分配一些唯一的值,并使用此值作为序列。更多信息在这里- http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/->非顺序索引

Update -使用非顺序索引方法总是更好,因为前面提到的第一种方法会在存在删除功能的情况下中断,该功能会返回删除中间的一个项,从而破坏序列。