2015-04-16 57 views
3

我目前正在ASP.NET MVC中創建一個網站,我想使用EditorTemplates在我的ViewModel中顯示List。 RowViewModel具有RowDataViewModel的列表。我試圖爲RowViewModel創建視圖,我可以使用未使用ASP.NET MVC EditorTemplate

@Html.EditorFor(model => model.RowData) 

因此我需要一個EditorTemplate。我創建並EditorTemplate下:

Views/Shared/EditorTemplates/RowDataViewModel.cshtml

,我也試圖把EditorTemplates文件夾/主頁/視圖文件夾下,但似乎沒有任何工作。 editortemplate內沒有任何斷點。我想我以前就是這樣做的,但我可能會忘記一些事情。

任何想法?

RowViewModel:

public class RowViewModel 
{ 
    [Required] 
    [Display(Name="Name")] 
    public string Name { get; set; } 

    public List<RowDataViewModel> RowData { get; set; } 
} 

RowDataViewModel:

public class RowDataViewModel 
{ 
    public string Name { get; set; } 
    public string Value { get; set; } 
    public string DataType { get; set; } 
} 

EditorTemplate - RowDataViewModel.cshtml:

@using iDealWebRole.ViewModels 
@model RowDataViewModel 

@Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
<div class="form-group"> 
    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.Value, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(model => model.Value, "", new { @class = "text-danger" }) 
    </div> 
</div> 
+0

你的意思'@ Html.EditorFor(型號=> model.RowData)'(不'@ model.EditorFor(...)' )? –

+0

只是在我的帖子中輸入錯誤。現在糾正它:) –

+0

你在這裏顯示的代碼是好的,並將正常工作。必須有其他一些問題。 –

回答

-2

的問題是在這裏:@Html.EditorFor(model => model.RowData)

你應該用你的編輯器中RowData每一行,如:

@for (int i = 0; i < Model.RowData.Count(); i++) 
{ 
    @Html.EditorFor(model => Model.RowData[i]) 
} 
+0

這會生成重複的'id'(無效的html)和重複的'name'屬性,這些屬性不會在回發時綁定到集合。 –

+0

編輯答覆Stephen Muecke的評論。 – saniokazzz

相關問題