2014-03-05 74 views
0

我有一個顯示待開票付款清單的頁面,付款分組爲客戶,並且每個組都有一個Ajax表單。當我在第一組發票的頁面上提交第一個表單時,它將正確回發,並將填充模型發送到我的控制器進行處理。如果我提交任何其他表單,表單會回傳所有數據(我可以使用firebug查看它的全部數據),但是MVC在調用控制器方法時沒有填充我的模型,它只是返回null。MVC for循環中的Ajax表單僅適用於第一個循環項目

筆者認爲:

@for (int i = 0; i < Model.InvoicingReportModels.Count(); i++) 
{ 
    @Html.EditorFor(m => m.InvoicingReportModels[i], "_InvoicingReportModel") 
} 

_InvoiceReportMode.cshtml

@using (Ajax.BeginForm(new AjaxOptions() { HttpMethod = "POST", OnSuccess = "InvoiceSaved", OnFailure="InvoiceSaveFailed" })) 
{ 
    @Html.HiddenFor(m => m.DisplaySummary) 
    @Html.HiddenFor(m => m.MasterAccountID) 
    <div class="settings_bordered_box" style="margin-bottom: 20px;"> 
     <div class="settings"> 
      <table> 
       <tr> 
        <th> 
         @Html.DisplayFor(m => m.DisplaySummary) 
         <span class="settings_box_divider">| </span> 
         <span>@Html.Raw(Model.Payments.Count().ToString()) Invoices</span> 
         <span class="settings_box_divider">| </span> 
         @Html.DisplayFor(m => m.TotalPrice) 
        </th> 
       </tr> 
      </table> 
     </div> 
     <div class="settings"> 
      <table> 
       <tr> 
        <th> 
         <input type="checkbox" id="@Html.IdForModel()_chkAll" class="selectAll" /></th> 
        <th>PaymentID</th> 
        <th>Date</th> 
        <th>Total Price</th> 
        <th>Payment Method</th> 
        <th>Invoice Number</th> 
        <th>&nbsp;</th> 
       </tr> 
       @for (int i = 0; i < Model.Payments.Count; i++) 
       { 
        Model.Payments[i].InvoiceNumberFieldID = Html.IdFor(m => m.Payments[i].InvoiceNumber).ToString(); 
        <tr> 
         <td style="text-align: center; vertical-align: middle;">@Html.CheckBoxFor(m => m.Payments[i].Selected) @Html.HiddenFor(m => m.Payments[i].SingleSaveSelected)</td> 
         <td style="text-align: center; vertical-align: middle;">@Html.DisplayFor(m => m.Payments[i].DisplayPaymentID) @Html.HiddenFor(m => m.Payments[i].PaymentID) @Html.HiddenFor(m => m.Payments[i].PaymentType)</td> 
         <td style="text-align: center; vertical-align: middle;">@Html.DisplayFor(m => m.Payments[i].Date)</td> 
         <td style="text-align: center; vertical-align: middle;">@Html.DisplayFor(m => m.Payments[i].TotalPrice)</td> 
         <td style="text-align: center; vertical-align: middle;">@Html.DisplayFor(m => m.Payments[i].PaymentMethod)</td> 
         <td style="text-align: center; vertical-align: middle;">@Html.EditorFor(m => m.Payments[i].InvoiceNumber) @Html.HiddenFor(m => m.Payments[i].InvoiceNumberFieldID)</td> 
         <td style="text-align: center; vertical-align: middle;"> 
          <input type="submit" name="submit" class="coloured_button" value="Save" onclick="SetSingleSave('@Html.IdFor(m => m.Payments[i].SingleSaveSelected)'); return true;" /> 
          &nbsp;&nbsp; 
         <input type="button" class="coloured_button" value="Details" /> 
         </td> 
        </tr> 
       } 
       <tr> 
        <td style="text-align: center; vertical-align: middle;">&nbsp;</td> 
        <td style="text-align: center; vertical-align: middle;">&nbsp;</td> 
        <td style="text-align: center; vertical-align: middle;">&nbsp;</td> 
        <td style="text-align: center; vertical-align: middle;">&nbsp;</td> 
        <td style="text-align: center; vertical-align: middle;">&nbsp;</td> 
        <td style="text-align: center; vertical-align: middle;">@Html.EditorFor(m => m.InvoiceNumber)</td> 
        <td style="text-align: center; vertical-align: middle;"> 
         <input type="submit" name="submit" class="coloured_button" value="Save Selected" /> 
        </td> 
       </tr> 
      </table> 
     </div> 
    </div> 
} 

我的控制器

[HttpPost] 
public ActionResult Index(InvoicingModel model, string submit) 
{ 
    ... Do controller stuff here 
} 

我的模型

public class InvoicingModel 
{ 
    public List<InvoicingReportModel> InvoicingReportModels { get; set; } 
    public InvoicingModel() 
    { 
     InvoicingReportModels = new List<InvoicingReportModel>(); 
    } 
} 

public class InvoicingReportModel 
    { 
     public int MasterAccountID { get; set; } 
     public string DisplaySummary { get; set; } 
     public string InvoiceNumber { get; set; } 
     [DataType(DataType.Currency)] 
     public double TotalPrice 
     { 
      get 
      { 
       if (Payments != null && Payments.Count > 0) 
       { 
        return Payments.Sum(p => p.TotalPrice); 
       } 
       else 
       { 
        return 0.0; 
       } 
      } 
     } 
     public List<PaymentListModel> Payments { get; set; } 

     public InvoicingReportModel() 
     { 
      Payments = new List<PaymentListModel>(); 
     } 
    } 

public class PaymentListModel 
{ 
    public int PaymentID { get; set; } 
    public string DisplayPaymentID { get; set; } 
    [DataType(DataType.Currency)] 
    public double TotalPrice { get; set; } 
    public string PaymentMethod { get; set; } 
    public string InvoiceNumber { get; set; } 
    public bool Selected { get; set; } 
    public bool SingleSaveSelected { get; set; } 
    public DateTime Date { get; set; } 

    public string InvoiceNumberFieldID { get; set; } 
    public PaymentType PaymentType { get; set; } 
} 

第一種形式發回InvoicingModel填充了一個InvoicingReportModel和必需數量的PaymentListModel全部填充。如果它提交第2個或更多的表單,則控制器獲取InvoicingReportModel列表爲空的InvoicingModel。

我已經嘗試更改控制器,以便它接受一個I​​nvoicingReportModel(因爲這是什麼形式)左右,但然後我什麼都沒有。

不知道還有什麼可以嘗試的。

如果有幫助,這裏是從提交頁面上的第三個表單發送的發佈數據的螢火蟲捕獲。

Parametersapplication/x-www-form-urlencoded 
InvoicingReportModels[2].... 1316 - Some Thing Here 
InvoicingReportModels[2].... 123 
InvoicingReportModels[2].... 1316 
InvoicingReportModels[2]....  
InvoicingReportModels[2].... InvoicingReportModels_2__Payments_0__InvoiceNumber 
InvoicingReportModels[2].... 13276 
InvoicingReportModels[2].... Standard 
InvoicingReportModels[2].... true 
InvoicingReportModels[2].... false 
InvoicingReportModels[2].... False 
InvoicingReportModels[2]....  
InvoicingReportModels[2].... InvoicingReportModels_2__Payments_1__InvoiceNumber 
InvoicingReportModels[2].... 13298 
InvoicingReportModels[2].... Standard 
InvoicingReportModels[2].... true 
InvoicingReportModels[2].... false 
InvoicingReportModels[2].... False 
X-Requested-With XMLHttpRequest 
submit Save Selected 
Source 
submit=Save+Selected&InvoicingReportModels%5B2%5D.DisplaySummary=1316+-+Some+thing+Here&InvoicingReportModels%5B2%5D.MasterAccountID=1316&InvoicingReportModels%5B2%5D.Payments%5B0%5D.Selected=true&InvoicingReportModels%5B2%5D.Payments%5B0%5D.Selected=false&InvoicingReportModels%5B2%5D.Payments%5B0%5D.SingleSaveSelected=False&InvoicingReportModels%5B2%5D.Payments%5B0%5D.PaymentID=13276&InvoicingReportModels%5B2%5D.Payments%5B0%5D.PaymentType=Standard&InvoicingReportModels%5B2%5D.Payments%5B0%5D.InvoiceNumber=&InvoicingReportModels%5B2%5D.Payments%5B0%5D.InvoiceNumberFieldID=InvoicingReportModels_2__Payments_0__InvoiceNumber&InvoicingReportModels%5B2%5D.Payments%5B1%5D.Selected=true&InvoicingReportModels%5B2%5D.Payments%5B1%5D.Selected=false&InvoicingReportModels%5B2%5D.Payments%5B1%5D.SingleSaveSelected=False&InvoicingReportModels%5B2%5D.Payments%5B1%5D.PaymentID=13298&InvoicingReportModels%5B2%5D.Payments%5B1%5D.PaymentType=Standard&InvoicingReportModels%5B2%5D.Payments%5B1%5D.InvoiceNumber=&InvoicingReportModels%5B2%5D.Payments%5B1%5D.InvoiceNumberFieldID=InvoicingReportModels_2__Payments_1__InvoiceNumber&InvoicingReportModels%5B2%5D.InvoiceNumber=123&X-Requested-With=XMLHttpRequest 

回答

0

我算出瞭解決方案。要使用缺少數組元素的模型聯編程序,您需要爲每個名爲Index的數組元素設置一個隱藏字段。將表單移動到頂層視圖中,併爲每個迭代添加一個隱藏的索引元素,如下所示,所有工作。

@for (int i = 0; i < Model.InvoicingReportModels.Count; i++) 
{ 
    using (Ajax.BeginForm(new AjaxOptions() { HttpMethod = "POST", OnSuccess = "InvoiceSaved", OnFailure = "InvoiceSaveFailed" })) 
    { 
     @Html.Hidden("InvoicingReportModels.Index", i) 
     @Html.EditorFor(m => Model.InvoicingReportModels[i], "_InvoicingReportModel") 
    } 
} 
相關問題