编辑器用于不生成正确的 html
本文关键字:html 用于 编辑器 | 更新日期: 2023-09-27 18:37:26
我有一个 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:
公共类权限视图模型 { public int Id { get; set; }
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.配置配置不正确?
非常感谢您的帮助。
看起来您需要为"PermissionViewModel"类创建一个编辑器模板,就像现在一样,MVC 似乎对如何为如此复杂的对象制作编辑器感到困惑。
在提供视图的文件夹中,添加一个名为"编辑器模板"的文件夹
然后在该文件夹中添加新的分部视图。代码应为:
@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
也许你想自己做点什么?
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);
}