c# - 未使用 ASP.NET MVC EditorTemplate

标签 c# asp.net asp.net-mvc asp.net-mvc-4 razor

我目前正在使用 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/

相关文章:

c# - WCF。服务泛型方法

c# - 单击按钮后继续在列表中添加项目

c# - 无法加载文件或程序集 'EntityFramework' 或其依赖项之一。程序集可能已被篡改

c# - 使用 json 传递关联数组 : which type to expect in the controller?

asp.net - 更改 ASP.NET MVC 或 IIS 中的 URL 根路径

javascript - 使用 JavaScript 设置 gridview 内文本框的格式

c# - 为什么 ObjectDataSource 没有发送正确的参数值?

javascript - JavaScript 中的文本框值

asp.net-mvc - MVC 验证的单元测试

c# - 覆盖,覆盖功能