代码之家  ›  专栏  ›  技术社区  ›  Azhar Hussain

复选框列表始终为空MVC

  •  0
  • Azhar Hussain  · 技术社区  · 7 年前

    我发邮件有困难 checkboxes EditorFor MVC中的值。我有两个班:

    public class CompleteReceiving
     {
       public List<SiteOrderReceiving> order_detail { get; set; } //<- Data from DB. It contains all items for Order Receiving.
       public List<SomeClass> new_form { get; set; } //<- Fields for form
     }  
    
    public class SomeClass
     {
            [RegularExpression("^[0-9]*$", ErrorMessage = "Receive Quantity can only contain number")]
            public decimal? receive_quantity { get; set; }
    
            [RegularExpression("^[0-9]*$", ErrorMessage = "Damaged Quantity can only contain number")]
            public decimal? damaged_quantity { get; set; }  
    
            public bool IsSelected { get; set; }
     }
    

    这是我的观点:

        @for(int i = 0; i < Model.order_detail.Count; i++)
         {  
          <tr> 
            <td>@Html.CheckBoxFor(m => m.new_form[i].IsSelected, new { id = "site_rec_checkbox", @class= "site_rec_checkbox" })
           </td>  
           <td>@Html.EditorFor(m => m.new_form[i].receive_quantity, new { htmlAttributes = new { @class = "form-control fm", @autocomplete = "off", @autofocus = "autofocus", @placeholder = "Receiving Quantity" } })
             @Html.ValidationMessageFor(m => m.new_form[i].receive_quantity, "", new { @class = "text-danger" })
           </td>  
         }  
    

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult SiteOrderReceiving(CompleteReceiving sor)
     {
         //Code for post
     }  
    

    问题是每当我选择 checkbox index 1 2 这个 List 总是 null . 但这是我第一次选择 3 4 1 比它好用多了。 我不知道我做错了什么。
    任何帮助都将不胜感激。

    更新
    这是我的控制器 Action Code

     [HttpGet]
     public ActionResult SiteOrderReceiving(int? order_id)
     {
       var get_detail = (from oa in db.order_send
                         where oa.order_id == order_id 
                         select new SiteOrderReceiving()
                         {
                           quantity_send = oa.send_quantity,
                           date_send = oa.send_date,
                           order_id = oa.order_id
                         }).ToList();
      var a = new CompleteReceiving();
      a.order_detail = get_detail;
      return View(a);
    }  
    

    这是我的 View

    @using (Html.BeginForm("SiteOrderReceiving", "Site", FormMethod.Post, new { id = "receive_form" }))
    {
      @Html.AntiForgeryToken();
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    
    @for(int i = 0; i < Model.order_detail.Count; i++)
    {  
     <tr> 
         <td>@Html.CheckBoxFor(m => m.new_form[i].IsSelected, new { id = "site_rec_checkbox", @class= "site_rec_checkbox" })
         </td>  
         <td>@Html.EditorFor(m => m.new_form[i].receive_quantity, new { htmlAttributes = new { @class = "form-control fm", @autocomplete = "off", @autofocus = "autofocus", @placeholder = "Receiving Quantity" } })
             @Html.ValidationMessageFor(m => m.new_form[i].receive_quantity, "", new { @class = "text-danger" })
         </td>  
      } 
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Azhar Hussain    7 年前

    所以我解决了我的问题。我所做的是移动我的 form DB .
    现在我的 ViewModel

    public class CompleteReceiving
    {
      public List<SomeClass> order_detail { get; set; }  
      //Some Other properties
    } 
    

    然后:

    public class SomeClass
    {  
       [RegularExpression("^[0-9]*$", ErrorMessage = "Receive Quantity can only contain number")]
       public decimal? receive_quantity { get; set; }
    
       [RegularExpression("^[0-9]*$", ErrorMessage = "Damaged Quantity can only contain number")]
       public decimal? damaged_quantity { get; set; }  
    
       public bool IsSelected { get; set; }    
    
       public string item_name { get; set; }
    } 
    

    code 就像上面所说的,一切都很顺利。希望对将来的人有帮助。