你提出问题的方式,恐怕任何答案都会导致一个相当复杂的观点。根据用户的角色决定显示哪个视图(以及构建哪个视图模型)是控制器的责任。
编辑1:对评论的回应
你能做这样的事情吗?
<% if (Model.AllowEdit) { %>
<%= Html.TextBoxFor(x => x.SomeProperty); %>
<% } else if (Model.AllowView) { %>
<%= Html.Encode(Model.SomeProperty) %>
<% } else { %>
<span>You may not view this property.</span>
<% } %>
这可以转化为辅助控件。
public static ExtensionsOfHtmlHelper
{
public static MvcHtmlString DynamicTextBox(this HtmlHelper html, Func<TModel, object> lambda, bool edit, bool view)
{
if (edit)
{
return html.TextBoxFor(lambda);
}
else if (view)
{
return html.LabelFor(lambda);
}
else
{
return MvcHtmlString.Create("<span>You may not view this value.</span>");
}
}
}
那么,在你看来,
<%= Html.DynamicTextBox(x => x.SomeProperty, Model.AllowEdit, Model.AllowView) %>
与之接近的东西应该可以工作。