我目前正在使用 ASP.NET MVC 创建一个网站,我想在其中使用 EditorTemplates 在我的 ViewModel 中显示一个列表。 RowViewModel 有一个 RowDataViewModel 列表。我正在尝试为 RowViewModel 创建 View ,我可以在其中使用
@Html.EditorFor(model => model.RowData)
因此我需要一个 EditorTemplate。我在以下位置创建了 EditorTemplate:
Views/Shared/EditorTemplates/RowDataViewModel.cshtml
我还尝试将 EditorTemplates 文件夹放在/Home/view 文件夹下,但似乎没有任何效果。编辑器模板内没有断点。我想我以前是这样做的,但我可能会忘记一些事情。
有什么想法吗?
行 View 模型:
public class RowViewModel
{
[Required]
[Display(Name="Name")]
public string Name { get; set; }
public List<RowDataViewModel> RowData { get; set; }
}
行数据 View 模型:
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>
最佳答案
问题就在这里:@Html.EditorFor(model => model.RowData)
您应该为 RowData 中的每一行使用您的编辑器,例如:
@for (int i = 0; i < Model.RowData.Count(); i++)
{
@Html.EditorFor(model => Model.RowData[i])
}
关于c# - 未使用 ASP.NET MVC EditorTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668623/