2016-09-04 25 views
0

我能夠成功地接受一個條目到我的數據庫。但是,其餘條目不是來自我的動態添加字段。如何使動作接受模型列表? (ASP.NET MVC 5)

這裏是我創建兄弟姐妹:

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

+0

你在哪裏調用「Create」方法? –

+0

你可以做類似Html.Action(「ActionName」,「ControllerName」,Model) –

+0

你可以用這個信息編輯問題嗎? – Kira

回答

1

你初始值爲x爲1,並且在將它傳遞給您在javascript中創建的動態html之前,您正在執行x++;這會將元素渲染爲siblings[2].child_id。這會破壞索引的連續性,因爲aiblings [1]缺失,並且模型綁定器將在序列中斷時停止綁定。

作爲一個解決方案,

更新 - 它始終是更好地去與非循序索引方法,因爲提到將在情況下打破第一種方法是有刪除功能,這將inturn刪除中間的一個項目,從而破壞序列。

+0

非常感謝您在我的Javascript代碼中指出這一點!我沒有注意到它。這樣的輕微故障。 – Fritz

+0

發生這種情況;)有時候我們需要的只是一雙額外的眼睛。乾杯。 – Developer

+0

這根本行不通 - 只是刪除其中一個項目併發布它(綁定失敗) –