使用模型方法创建共享视图

发布于 2024-08-11 02:37:43 字数 109 浏览 8 评论 0原文

我想创建一个共享控件,在其中根据登录用户的角色生成标记。为了实现这一点,我需要调用模型类中的方法。这是在 ASP.NET MVC 中执行此操作的正确方法吗,因为我听说我们应该严格分离模型和视图。 请帮忙。

I want to create a shared control where i want to generate markup according to the role of logged in user. TO achieve this I need to call a method in Model class. Is this a right way to do this in ASP.NET MVC as I heard that we should strictly separate out Model and Views.
Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情洁癖 2024-08-18 02:37:43

在我看来,视图不应该知道角色应该看到什么。这是一个控制器功能。同样,模型不应该关心视图如何使用其信息。

所以,这就是我的设计方式。

视图请求信息。控制器应该知道用户是谁,以及他们应该看到什么。控制器要求模型向视图提供正确的内容。

如果您可以设计系统,使其无需模型或视图即可显现出来,那就太好了。

In my opinion, the View should not know what a Role should see. It is a Controller function. In the same way, Model should not concern itself with how the View uses its information.

So, here is how I design it.

The View asks for information. The controller should know who the user is, and what they should see. The controller asks the Model to give the right stuff to the View.

It is nice if you could design the system so that it can manifest itself with no Model or View.

原谅过去的我 2024-08-18 02:37:43

模型和视图的分离只能达到通信方向只能朝一个方向的程度。通常,视图会了解底层模型,但反之则不然。

这就是为什么 ASP.NET MVC 具有强类型视图的概念,其中视图可以强绑定到特定类型的模型。

因此,我认为将视图耦合到模型类的成员没有任何问题。

The Models and Views should only be separated to the extend that the direction of communication should go in only one direction. Typically, a View will know about the underlying Model, but not the other way around.

This is why ASP.NET MVC has the concept of strongly typed View where Views can be strongly bound to a particular type of Model.

As such, I don't see any problem with coupling your View to a member of the Model class.

这样的小城市 2024-08-18 02:37:43

虽然可以通过辅助扩展读取模型。创建大量标记是否需要这样做?如果是这样,我会质疑这是否是最佳方法?

您可以考虑使用“asp:loginview”并从这里呈现部分视图?不依赖视图状态的 ASP.NET 控件在 MVC 中可以正常工作。

例子:

<asp:LoginView id="LoginView1" runat="server">
                <RoleGroups>
                    <asp:RoleGroup Roles="Admin">
                        <ContentTemplate>
                            <%= Html.RenderPartial("MyPartial"); %>
                        </ContentTemplate>
                    </asp:RoleGroup>
                </RoleGroups>
            </asp:LoginView>

Whilst it's fine to read from your model via a helper extension. is this required to create large amounts of markup? if so i would question if this is the optimal approach?

You could consider using the "asp:loginview" and render partial views from here? ASP.NET Controls that dont rely on viewstate work fine in MVC.

Example:

<asp:LoginView id="LoginView1" runat="server">
                <RoleGroups>
                    <asp:RoleGroup Roles="Admin">
                        <ContentTemplate>
                            <%= Html.RenderPartial("MyPartial"); %>
                        </ContentTemplate>
                    </asp:RoleGroup>
                </RoleGroups>
            </asp:LoginView>
酒浓于脸红 2024-08-18 02:37:43

在 MVC Futures 中,您还拥有 Html 帮助器 RenderAction,它允许您的视图调用和操作,然后在其自己的标记内呈现该操作的结果。

这对于做菜单之类的事情很有帮助,但这不是严格的 MVC,但它非常实用。

但最好的使用方法取决于您需要输出的 html、需要输出的位置以及原因。如果您能告诉我们更多有关您需要渲染的 Html 及其用途的信息,那就太好了,我们可以为您提供更好的帮助。

但有一件事是肯定的,模型不应该输出 Html 来渲染。

In MVC Futures you also have the Html helper RenderAction, which allows your view to call and action and it then renders the result of that action within its own mark up.

This is helpful for doing things like menu's and so on, but this is not strictly MVC, but it is very practical.

But the best method to use is going to depend on the html you need to output, where you need to output it and why. If you could tell us a bit more about the Html you need to render and its purpose that would be good and we can give you some better help.

But one thing is for sure, Models should not be outputing Html to render.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文