我正在尝试基于用户权限(MVC版本5.2.3)使一些字段可编辑和一些字段为只读。我可以在@html.EditorFor()上看到关于这个主题的一大堆答案,但不是普通的老式@html.Editor()。我尝试过以下几种方法,没有一种方法产生只读字段:
@Html.Editor(property.Name, new { @disabled = "true" })
@Html.Editor(property.Name, new { @disabled = "disabled" })
有谁可以帮我?非常感谢。
发布于 2018-07-25 06:34:45
您可以编写自己的扩展来接收布尔属性"disabled"
作为参数:
public static class HtmlExtensions
{
public static MvcHtmlString EditorDisabled<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool disabled, object htmlAttributes = null)
{
return EditorDisabled(htmlHelper, expression, disabled, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString EditorDisabled<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool disabled, IDictionary<string, object> htmlAttributes)
{
if (htmlAttributes == null)
htmlAttributes = new Dictionary<string, object>();
if (disabled)
htmlAttributes["disabled"] = "disabled";
return htmlHelper.Editor(expression, htmlAttributes);
}
}
发布于 2018-07-26 04:41:13
您只需将其从Editor更改为:
@Html.DisplayFor(model => model.Title)
它只会从你的模型中提取值。从那时起,你只需要在razor中实现关于谁可以看到什么的逻辑。例如:
if(Request.IsAuthenticated){
if (isAdmin){
@Html.Editor(model => model.Title)
}
if (isUser){
@Html.DisplayFor(model => model.Title)
}
}
我希望这能帮助你走上正确的道路!
https://stackoverflow.com/questions/51508308
复制相似问题