0

我有一个 asp.net mvc 2 应用程序。我需要向每个用户显示相同的页面。但是每个用户对数据拥有不同的权利。IE 有些可以看到但不能编辑某些数据,有些不能编辑也看不到数据。理想情况下,无法查看或编辑的数据是视图上的空白。出于安全原因,我希望我的视图模型尽可能稀疏。我的意思是如果一个字段不能被看到或编辑,则该字段不应该在视图模型上。显然我可以为每个视图模型编写视图,但这似乎很浪费。所以这是我的想法/愿望清单

我可以用属性装饰视图模型并挂钩到 html 助手的预渲染事件并告诉它这样做吗 ???

我可以 为在视图模型上找不到的条目输出 html 助手吗?

或者

我可以轻松地将内置的视图转换为代码,然后以编程方式构建标记,然后放入渲染引擎以在客户端处理和查看为 html 吗?

4

1 回答 1

0

你提出问题的方式,恐怕任何答案都会导致一个相当复杂的观点。根据用户的角色决定显示哪个视图(以及构建哪个视图模型)是控制器的责任。

编辑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) %>

与之接近的东西应该可以工作。

于 2010-03-29T16:16:26.157 回答