【问题标题】:EditorFor not generating proper htmlEditorFor 没有生成正确的 html
【发布时间】:2012-06-15 19:36:09
【问题描述】:

我有一个 mvc web 项目,我尝试使用 EditorFor 扩展方法呈现复选框列表,但结果只是将 id 显示为文本而不是复选框列表。

这是视图中的代码:

  <div id="permissions" class="tab-body">
     @Html.Label("Permissions :")
     @Html.EditorFor(x => Model.Permissions)
     <br />
     <br />
  </div>

这是对象“模型”的属性“权限”:

  [DisplayName("Permissions")]
  public List<PermissionViewModel> Permissions { get; set; }

这是 PermissionViewModel:

公共类 PermissionViewModel { 公共 int ID { 获取;放; }

  public UserGroupPermissionType Name { get; set; }

  public string Description { get; set; }

  public bool IsDistributable { get; set; }

  public bool IsGranted { get; set; }

}

最后,这是浏览器中的结果:

<div id="permissions" class="tab-body" style="display: block;">
<label for="Permissions_:">Permissions :</label>
192023242526272829
<br>
<br>
</div>

您知道为什么没有正确生成 html 吗?缺少依赖项?依赖冲突? Web.Config 配置不正确?

非常感谢您的帮助。

【问题讨论】:

标签: c# asp.net-mvc-3 viewmodel data-annotations editorfor


【解决方案1】:

看起来您需要为“PermissionViewModel”类创建一个编辑器模板,因为现在,MVC 似乎对如何为如此复杂的对象制作一个编辑器感到困惑。

在提供视图的文件夹中,添加一个名为“EditorTemplates”的文件夹

然后在该文件夹中添加一个新的局部视图。代码应该是:

@model IEnumberable<PermissionViewModel>
@foreach(var permission in Model)
@Html.EditorFor(x => x.Name)
@Html.EditorFor(x => x.Description)
@Html.EditorFor(x => x.IsDistributable)
@Html.EditorFor(x => x.IsGranted)

您还需要为 Name 类创建一个编辑器模板。

所以现在在你看来你可以调用

<div id="permissions" class="tab-body">
 @Html.Label("Permissions :")
 @Html.EditorFor(x => Model.Permissions)
 <br />
 <br />
</div>

并且 MVC 会知道使用您刚刚为您的许可制作的编辑器模板。

了解编辑器模板的好资源在这里:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

【讨论】:

  • 这将允许一个漂亮的显示,但将其解析回模型可能会有点棘手。
  • @JeremyHolovacs 如果一切都正确连接,模型绑定应该在发布后将它们放在一起。事实上,当您在 int 或字符串上执行 EditorFor 时,MVC 使用默认的编辑器模板,并且定义您自己的应该不会导致任何问题。
  • @Brenton,你给了我一个很好的指示,为什么我的页面没有正确呈现!在我的项目中搜索后,我发现我在 Views 文件夹下有一个 EditorTemplates 文件夹,该文件夹包含许多名为 viewModel 对象的文件。只有一个问题;最近,我更改了我的 viewModel 对象的所有名称以添加后缀“ViewModel”。在那之后,我认为 Mvc 无法将模板文件与相应的模型对象匹配,因为“ViewModel”后缀。它现在完美运行!感谢您的帮助。非常感谢! :)
  • 对于其他想要我的问题示例的编辑器模板文件示例的人:
    抱歉无法在当前评论中格式化代码。
    @Html.CheckBoxFor(m => m.IsGranted)
    @ Html.HiddenFor(m => m.Id)
    @Html.LabelFor(m => m.IsGranted, Model.Name.GetEnumDescription( )) @Html.DisplayTextFor(model => model.Description)

【解决方案2】:

也许你想自己做点什么?

    public delegate object Property<T>(T property);

    public static HtmlString MultiSelectListFor<TModel, TKey, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, IEnumerable<TKey>>> forExpression,
        IEnumerable<TProperty> enumeratedItems,
        Func<TProperty, TKey> idExpression,
        Property<TProperty> displayExpression,
        Property<TProperty> titleExpression,
        object htmlAttributes) where TModel : class
    {
        //initialize values
        var metaData = ModelMetadata.FromLambdaExpression(forExpression, htmlHelper.ViewData);
        var propertyName = metaData.PropertyName;
        var propertyValue = htmlHelper.ViewData.Eval(propertyName).ToStringOrEmpty();
        var enumeratedType = typeof(TProperty);

        //check for problems
        if (enumeratedItems == null) throw new ArgumentNullException("The list of items cannot be null");

        //build the select tag
        var returnText = string.Format("<select multiple=\"multiple\" id=\"{0}\" name=\"{0}\"", HttpUtility.HtmlEncode(propertyName));
        if (htmlAttributes != null)
        {
            foreach (var kvp in htmlAttributes.GetType().GetProperties()
             .ToDictionary(p => p.Name, p => p.GetValue(htmlAttributes, null)))
            {
                returnText += string.Format(" {0}=\"{1}\"", HttpUtility.HtmlEncode(kvp.Key),
                 HttpUtility.HtmlEncode(kvp.Value.ToStringOrEmpty()));
            }
        }
        returnText += ">\n";

        //build the options tags
        foreach (TProperty listItem in enumeratedItems)
        {
            var idValue = idExpression(listItem).ToStringOrEmpty();
            var displayValue = displayExpression(listItem).ToStringOrEmpty();
            var titleValue = titleExpression(listItem).ToStringOrEmpty();
            returnText += string.Format("<option value=\"{0}\" title=\"{1}\"",
                HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue));
            if (propertyValue.Contains(idValue))
            {
                returnText += " selected=\"selected\"";
            }
            returnText += string.Format(">{0}</option>\n", HttpUtility.HtmlEncode(displayValue));
        }

        //close the select tag
        returnText += "</select>";
        return new HtmlString(returnText);
    }

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多