2017-08-15 43 views
1

尽管纳入了我在其他问题中找到的所有建议,并且this article 列表vsValues传递给视图后POST始终为空。ASP.NET集合在POST后为空

查看

@model OTS.ParcelOrder 
 

 
@{ 
 
    ViewBag.Title = "Create"; 
 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
 
} 
 

 

 
<h2>Create</h2> 
 

 
@using (Html.BeginForm()) 
 
{ 
 
    @Html.AntiForgeryToken() 
 
    
 
    <div class="form-horizontal"> 
 
     <h4>ParcelOrder</h4> 
 
     <hr /> 
 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
 
     <div class="form-group"> 
 
      @Html.LabelFor(model => model.otsID, htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.EditorFor(model => model.otsID, new { htmlAttributes = new { @class = "form-control" } }) 
 
       @Html.ValidationMessageFor(model => model.otsID, "", new { @class = "text-danger" }) 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      @Html.LabelFor(model => model.parcelID, htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.EditorFor(model => model.parcelID, new { htmlAttributes = new { @class = "form-control" } }) 
 
       @Html.ValidationMessageFor(model => model.parcelID, "", new { @class = "text-danger" }) 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      @Html.LabelFor(model => model.recipientCountry, htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.EditorFor(model => model.recipientCountry, new { htmlAttributes = new { @class = "form-control" } }) 
 
       @Html.ValidationMessageFor(model => model.recipientCountry, "", new { @class = "text-danger" }) 
 
      </div> 
 
     </div> 
 

 

 
     @for (int i = 0; i < Model.vsValues.Count; i++) 
 
     { 
 
      @Html.Label(Model.ParcelOrder_VSFields.ElementAt(i).VendorSpecifiedInfoField.fieldName, 
 
      htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.EditorFor(model => model.vsValues[i], new { htmlAttributes = new { @class = "form-control" } }) 
 
       @Html.ValidationMessageFor(model => model.vsValues[i], "", new { @class = "text-danger" }) 
 
      </div> 
 
     } 
 

 

 
     <div class="form-group"> 
 
      <div class="col-md-offset-2 col-md-10"> 
 
       <input type="submit" value="Create" class="btn btn-default" /> 
 
      </div> 
 
     </div> 
 
    </div> 
 
} 
 

 
<div> 
 
    @Html.ActionLink("Back to List", "Index") 
 
</div>

控制器

 // GET: ParcelOrders/Create 
 
     public ActionResult Create(int vendorId = 1) 
 
     { 
 
      ParcelOrder order = new ParcelOrder(vendorId); 
 
      return View(order); 
 
     } 
 

 
     // POST: ParcelOrders/Create 
 
     // Aktivieren Sie zum Schutz vor übermäßigem Senden von Angriffen die spezifischen Eigenschaften, mit denen eine Bindung erfolgen soll. Weitere Informationen 
 
     // finden Sie unter http://go.microsoft.com/fwlink/?LinkId=317598. 
 
     [HttpPost] 
 
     [ValidateAntiForgeryToken] 
 
     public ActionResult Create(ParcelOrder parcelOrder) 
 
     { 
 
      parcelOrder.customerID = User.Identity.GetUserId(); 
 
      if (ModelState.IsValid) 
 
      { 
 
       db.ParcelOrder.Add(parcelOrder); 
 
       db.SaveChanges(); 
 
       return RedirectToAction("Index"); 
 
      } 
 

 
      return View(parcelOrder); 
 
     }

public partial class ParcelOrder 
{ 
    private Entities db = new Entities(); 
    public List<string> vsValues = new List<string>(); 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public ParcelOrder() 
    { 
     this.ParcelOrder_VSFields = new List<ParcelOrder_VSFields>(); 
    } 


    public ParcelOrder(int vendorId) 
    { 
     this.ParcelOrder_VSFields = new List<ParcelOrder_VSFields>(); 

     var vendorQuery = from vsif in db.VendorSpecifiedInfoField 
          where vsif.vendorID == vendorId 
          select vsif; 

     foreach (var vsif in vendorQuery) 
     { 
      vsValues.Add(""); 
      this.ParcelOrder_VSFields.Add(new OTS.ParcelOrder_VSFields 
      { 
       vsFieldID = vsif.id, 
       VendorSpecifiedInfoField = vsif, 
       value = "" 
      }); 
     } 
    } 

    public string otsID { get; set; } 
    public string parcelID { get; set; } 
    public string customerID { get; set; } 
    public string recipientCountry { get; set; } 

    public virtual AspNetUsers AspNetUsers { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<ParcelOrder_VSFields> ParcelOrder_VSFields { get; set; } 
} 

}

的值应该在列表vsValues被张贴和稍后将设定为ParcelOrder_VSFields控制器内部属性,以避免张贴冗余信息。

回答

1

这是因为你发布什么可没有地方看看那个

@using (Html.BeginForm()) 

而应是这样:

@using (Html.BeginForm("Create","ParcelOrders",FormMethod.Post)) 

更新

之后也。你的模型在我看来是错误的,如果你想将值传递给列表,我建议你有一个同类的列表属性[这个属性需要在你的模型ParcelOrder而不是虚拟]。然后在类的无参数构造函数中做你的foreach。在你看到你的问题的每一步中追踪它。

+0

指定POST方法既没有改变情况(列表仍为空),也没有改变表单的结果HTML代码(它具有有效的HTTP方法和指定的路径)。 原始字段(如otsId)也正确发布。 – user2052244

+0

@ user2052244查看更新 – Valkyrie

+0

您对与模型相关的问题是正确的。 在空的构造函数中填充列表解决了这个问题。 – user2052244