2016-09-21 102 views
0

我錯過了一件事,同時從視圖到控制器傳遞一個列表。它在控制器的[HttpPost]方法中顯示爲空。任何人請指導,我如何獲得從視圖到控制器的列表數據。請在下面查看我的完整代碼。如何通過列表從視圖到控制器 - MVC 4

@model List<payorder_draft_printing.Models.registration> 

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

<div class="container"> 

    <div class="row" style="text-align: left"> 

     <h2><u>Bulk Approval</u></h2> 

     <br /> 
     <br /> 

     @using (Html.BeginForm("bulk_approval", "Sms", FormMethod.Post, new { enctype = "multipart/form-data" })) 
     { 
      <div style="width: 700px;" align="center"> 

       <table id="GetSerial" class="table"> 
        <thead> 
         <tr class="ui-widget-header"> 
          <th>Account Number</th> 
          <th>Mobile Number</th> 
          <th>Customer Name</th> 
          <th>Branch Code</th> 
          <th>Bulk Upload</th> 
          <th>Create Date</th> 
          <th>Created By</th> 
          <th>Active</th> 
          </tr> 
        </thead> 

        <tbody> 

         @if (Model != null) 
         { 
          foreach (var m in Model) 
          { 
          <tr style="height: 25px; border-bottom: 1px solid gray"> 
           <td style="min-width: 120px">@m.account_number</td> 
           <td style="min-width: 120px; width: 450px;">@m.mobile_number</td> 
           <td style="min-width: 250px; width: 250px">@m.customer_name</td> 
           <td style="min-width: 100px; width: 100px">@m.BranchCode</td> 
           <td style="min-width: 100px; width: 100px">@m.BulkUpload</td> 
           <td style="min-width: 150px;">@string.Format("{0:dd-MMM-yyyy}", @m.create_date)</td> 
           <td style="min-width: 100px;">@m.created_by</td> 
           <td style="min-width: 100px; width: 100px">@m.Active</td> 
          </tr> 
          } 
         } 

        </tbody> 

       </table> 
       <input type="submit" value="Update" /> 
      </div> 
     } 

    </div> 

</div> 

在下面的代碼中,我試圖從視圖中獲取提交列表到控制器,但其結果爲空。

[HttpPost] 
      public ActionResult bulk_approval(List<registration> model)//here my model shows null, please guide. 
      { 
       foreach (var abc in model) 
       { 

       } 
       return View(); 
      } 

以下是我的課。

public class registration 
{ 
    public int Id { get; set; } 
    public string mobile_number { get; set; } 
    public string account_number { get; set; } 
    public string customer_name { get; set; } 
    public int FrequencyId { get; set; } 
    public bool Active { get; set; } 
    public string BranchCode { get; set; } 
    public bool BulkUpload { get; set; } 
    public string created_by { get; set; } 
    public DateTime create_date { get; set; } 
} 
+0

使用索引('for'環)而不是'foreach'循環 – devqon

+0

能否請您分享示例代碼, 謝謝。 –

+0

[ADO.NET DataTable的HTML表格]的可能的副本(http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable) –

回答

1

隨着foreach循環,MVC只是創建相同的輸入一遍又一遍,因爲它不知道他們應該是不同的。因此,您需要使用for -loops,並使用索引。我沒有看到任何輸入表單中的,所以我就舉一個例子:

@if (Model != null) { 
    for (var i = 0; i < Model.Count; i++) { 
     @Html.TextBoxFor(m => Model[i].SomeProperty) 
    } 
} 

另外,如果我沒記錯的話,你需要使用一個IList爲模型:

@model IList<payorder_draft_printing.Models.registration> 

爲了能夠發表您的(不可編輯的)文本,你需要添加隱藏的輸入:

@Model[i].account_number 
@Html.HiddenFor(m => Model[i].account_number) 
+0

感謝您的澄清現在我正在獲取值正確。現在如何顯示值的標籤形式而不是文本框,因爲您在代碼中使用了@ Html.TextBoxFor,謝謝 –

+2

爲什麼要發佈甚至不可編輯的內容?如果你沒有輸入,表單的想法會消失 – devqon

+0

你說得對,實際上我的情況是我只需要編輯最後一列[Status]布爾值,它是真或假,其餘列應該只是作爲標籤顯示,請指導如何做,謝謝。 –

相關問題