【问题标题】:How to make Action accept List for a Model? (ASP.NET MVC 5) [duplicate]如何使动作接受模型的列表? (ASP.NET MVC 5)[重复]
【发布时间】:2017-01-11 20:21:33
【问题描述】:

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

这是我为兄弟姐妹创建的:

 [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")

【问题讨论】:

  • 你在哪里调用了“Create”方法?
  • 你可以做类似 Html.Action("ActionName","ControllerName", Model)
  • 你能用这个信息编辑问题吗?
  • @AshkanSirous 这已经被我的观点处理了——在用户点击创建按钮之后。所以,调用“创建”方法对我来说根本不是问题。我只需要让我的“创建”方法完全接受列表。
  • 从客户端传递您的列表的代码在哪里?

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

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

作为解决方案,

更新 - 使用非顺序索引方法总是更好,因为如果有删除功能会破坏第一种方法,这会反过来删除中间的一个项目,从而破坏顺序.

【讨论】:

  • 非常感谢您在我的 Javascript 代码中指出这一点!我没有注意到它。这么小的故障。
  • 发生了 ;) 。有时我们需要的只是一双额外的眼睛。干杯。
  • 这根本不起作用 - 只需删除其中一项并发布它(绑定将失败)
  • @StephenMuecke - 为此我提到了另一种方法 - 非顺序索引,我将在我的回答中专门更新这一点,谢谢。
  • 我也将非顺序索引方法应用于我的代码。它现在完美无缺!
猜你喜欢
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多