简体   繁体   中英

ASP.NET MVC EditorTemplate not being used

I am currently making a website in ASP.NET MVC where i would like to use EditorTemplates to display a List inside my ViewModel. RowViewModel has a List of RowDataViewModels. I'm trying to make a Create View for RowViewModel, where i can use

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

and therefore i need an EditorTemplate. I created and EditorTemplate under:

Views/Shared/EditorTemplates/RowDataViewModel.cshtml

And i also tried putting the EditorTemplates folder under the /Home/ view folder, but nothing seems to work. No breakpoints are being hit inside the editortemplate. I think i did it this way before, but i might be forgetting something.

Any ideas?

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>

The problem is here: @Html.EditorFor(model => model.RowData)

You should use your editor for every row in RowData, like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM