2017-05-09 25 views
0

我想創建一個視圖,用戶可以同時更新(創建/編輯)多個日記帳分錄(每個日記帳一個)。如何爲列表屬性創建多個聯機表格

視圖模型:

public class CompanyRecoMonthViewModel 
{ 
    public Company Company { get; set; } 
    public string RecoMonth { get; set; } 
} 

公司擁有期刊的名單:

public virtual IList<Journal> Journals { get; set; } 

每個雜誌有JournalEntries

public IList<JournalEntry> JournalEntries { get; set; } 

在每個月的名單雜誌將有一個(或無)JournalEntry。

從控制器中,我將日誌加載到視圖中。

現在在視圖中的代碼。我試圖將內聯表單放入表格視圖中。 我的問題是,在提交表單時,沒有任何值被捕獲。

可能沒有正確使用@ Html.BeginForm。似乎表單不知道它要編輯哪個對象。

<table class="table"> 
    <tr> 
     <th> 
      Person Responsible 
     </th> 
     <th> 
      Journal Number 
     </th> 
     <th> 
      SomeID 
     </th> 
     <th> 
      Status 
     </th> 
     <th> 
      Upload Date 
     </th> 
     <th> 
      Amount< 
     </th> 
     <th></th> 
    </tr> 

    @foreach (var item in Model.Company.Journals) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.User.Name) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.JournalNumber) 
      </td> 

      <!-- this is where we start inline form for Journal Entry--> 
      @using (Html.BeginForm("Edit", "JournalEntries", FormMethod.Post, new { enctype = "multipart/form-data" })) 
      { 
       @Html.AntiForgeryToken() 
       <form class="form-inline"> 
        @Html.HiddenFor(modelItem => item.JournalID) 

        <div class="form-group"> 
         <td> 
          @Html.TextBoxFor(modelItem => item.JournalEntries[0].SomeID, new { @class = "form-control", maxlength = "8" }) 
         </td> 
         <td> 
          @Html.DropDownListFor(modelItem => item.JournalEntries[0].Status, Model.JournalStatuses.Select(e => new SelectListItem { Text = e }), string.Empty, new { @class = "form-control" }) 
         </td> 
         <td> 
          @Html.EditorFor(modelItem => item.JournalEntries[0].DatePosted, new { htmlAttributes = new { @class = "form-control datepicker" } }) 
         </td> 
         <td> 
          @Html.DropDownListFor(modelItem => item.JournalEntries[0].JournalType, Model.JournalTypes.Select(e => new SelectListItem { Text = e }), string.Empty, new { @class = "form-control" }) 
         </td> 
         <td> 
          @Html.EditorFor(modelItem => item.JournalEntries[0].AmountPosted, new { htmlAttributes = new { @class = "form-control" } }) 
         </td> 

         <td> 
          <button type="submit" class="btn btn-default">Save</button> 
         </td> 
        </div> 
       </form> 
      } 

     </tr> 
    } 

</table> 
+0

請發佈您的控制器POST方法 – User3250

回答

0

設法實現對於每個局部視圖對一個實體使用內聯表單的多個部分視圖。

0

添加虛擬關鍵字JournalEntries財產在你的日記類導航到JournalEntry的類。

public virtual IList<JournalEntry> JournalEntries { get; set; }