我正在使用MVC 3,Razor視圖和EditorFor模板。嵌套時出現錯誤EditorTempates
我有三個簡單的嵌套模式: -
public class BillingMatrixViewModel
{
public ICollection<BillingRateRowViewModel> BillingRateRows { get; set; }
public BillingMatrixViewModel()
{
BillingRateRows = new Collection<BillingRateRowViewModel>();
}
}
public class BillingRateRowViewModel
{
public ICollection<BillingRate> BillingRates { get; set; }
public BillingRateRowViewModel()
{
BillingRates = new Collection<BillingRate>();
}
}
public class BillingRate
{
public int Id { get; set; }
public int Rate { get; set; }
}
的BillingMatrixViewModel有一個觀點: -
@using System.Collections
@using WIP_Data_Migration.Models.ViewModels
@model WIP_Data_Migration.Models.ViewModels.BillingMatrixViewModel
<table class="matrix" id="matrix">
<tbody>
<tr>
@Html.EditorFor(model => Model.BillingRateRows, "BillingRateRow")
</tr>
</tbody>
</table>
的BillingRateRow有一個編輯模板稱爲BillingRateRow: -
@using System.Collections
@model IEnumerable<WIP_Data_Migration.Models.ViewModels.BillingRateRowViewModel>
@foreach (var item in Model)
{
<tr>
<td>
@item.BillingRates.First().LabourClass.Name
</td>
@Html.EditorFor(m => item.BillingRates)
</tr>
}
BillingRate有一個編輯模板: -
@model WIP_Data_Migration.Models.BillingRate
<td>
@Html.TextBoxFor(model => model.Rate,
new {style = "width: 20px"})
</td>
每個輸入所產生的標記是: -
<input name="BillingMatrix.BillingRateRows.item.BillingRates[0].Rate" id="BillingMatrix_BillingRateRows_item_BillingRates_0__Rate" style="width: 20px;" type="text" value="0"/>
公告的名稱和ID屬性的BillingRate索引很好地處理,但在BillingRateRows沒有索引而不是「.item。」。從我的研究來看,這是因爲由於foreach循環而導致上下文被取消,所以循環不應該是必需的。
我想要實現: -
<input name="BillingMatrix.BillingRateRows[0].BillingRates[0].Rate" id="BillingMatrix_BillingRateRows_0_BillingRates_0__Rate" style="width: 20px;" type="text" value="0"/>
如果我改變BillingRateRow觀: -
@model WIP_Data_Migration.Models.ViewModels.BillingRateRowViewModel
<tr>
@Html.EditorFor(m => Model.BillingRates)
</tr>
它會引發InvalidOperationException,「模型項目傳遞到字典的類型系統.Collections.ObjectModel.Collection [BillingRateRowViewModel],但是這個字典需要一種類型的BillingRateRowViewModel。
任何人都可以對此有所瞭解嗎?
我想你就在這裏,我做類似的事情解決變量BillingRateRows到BillingRateRowViewModels 在BillingMatrix視圖中將@ Html.EditorFor(model => Model.BillingRateRows,「BillingRateRow」)更改爲@ Html.EditorFor(model => Model.BillingRateRowViewModels) –