在部分视图中访问数据库信息,.ascx 包含在 asp.net mvc 的 Site.Master 中

发布于 2024-10-29 12:39:07 字数 1067 浏览 5 评论 0原文

嗨,我遇到了这个问题。我正在开发一个 asp.net mvc 2 应用程序。我定义了部分视图 menu.ascx 。这将包含在我网站的 Site.Master 母版页中的所有页面中。现在的问题是我希望我的菜单根据用户类型进行更改。 这是我一开始所做的:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<% 
    ExtendedMemberShip.MemberShipUser user = ExtendedMemberShip.MemberShip.GetUser(HttpContext.Current.User.Identity.Name);
    string course = "Course/Index/";
    if(user != null) course += user.UserName;
%>
<% 
     if(user!=null && user.Type == "stud") { 
%>
         <li><%: Html.ActionLink("Courses", "Index", course)%></li>
<% 
      }
%>
<li><%: Html.ActionLink("Votes", "About", "Home")%></li>
<li><%: Html.ActionLink("Comments", "About", "Home")%></li>
<li><%: Html.ActionLink("Exam archives", "About", "Home")%></li>

问题是我不应该在视图中这样做!但由于这是 MasterPage,没有控制器实际调用它,所以我不知道将信息放在 ViewData 字典或 ViewModel 中的何处以将其传递到此母版页... 有什么想法吗?

Hi i'm having problems with this. I am developing an asp.net mvc 2 application. I have a partial view menu.ascx defined. this gets included on all the pages of my site in the Site.Master masterpage. Now the thing is I want my menu to change according to the type of user.
Here's what I did at first:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<% 
    ExtendedMemberShip.MemberShipUser user = ExtendedMemberShip.MemberShip.GetUser(HttpContext.Current.User.Identity.Name);
    string course = "Course/Index/";
    if(user != null) course += user.UserName;
%>
<% 
     if(user!=null && user.Type == "stud") { 
%>
         <li><%: Html.ActionLink("Courses", "Index", course)%></li>
<% 
      }
%>
<li><%: Html.ActionLink("Votes", "About", "Home")%></li>
<li><%: Html.ActionLink("Comments", "About", "Home")%></li>
<li><%: Html.ActionLink("Exam archives", "About", "Home")%></li>

The problem with this is that I shouldn't be doing this in the view ! But since this is the MasterPage no controller actually calls it so I don't know where to put the info in the ViewData dictionnary or ViewModel to pass it to this masterpage...
any ideas?

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

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

发布评论

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

评论(1

梦毁影碎の 2024-11-05 12:39:07

您仍然可以在控制器中执行此操作。将相关数据填充到 ViewData 字典中:

public ActionResult Any()
{
    LoadUserType();
    return View();
}

private void LoadUserType()
{
    ExtendedMemberShip.MemberShipUser user = 
        ExtendedMemberShip.MemberShip.GetUser(HttpContext.Current.User.Identity.Name);
    ViewData["UserType"] = user.Type;
}

在母版页中使用:

<% Html.RenderPartial("Menu", ViewData["UserType"]) %>

您可以通过创建为您执行此操作的自定义属性或将其放入所有控制器扩展的基类。

或者,您可以只将菜单项填充为模型的一部分,并将模型传递给 PartialView。

You can still do this in your controller. Populate your the relevant data into the ViewData dictionary:

public ActionResult Any()
{
    LoadUserType();
    return View();
}

private void LoadUserType()
{
    ExtendedMemberShip.MemberShipUser user = 
        ExtendedMemberShip.MemberShip.GetUser(HttpContext.Current.User.Identity.Name);
    ViewData["UserType"] = user.Type;
}

In your master page use:

<% Html.RenderPartial("Menu", ViewData["UserType"]) %>

You could avoid having to call LoadUserType() or whatever by creating a Custom Attribute that did that for you, or putting it in a base class that all of your controllers extended.

Alternatively, you could just populate the menu items as part of the Model and pass the Model to the PartialView.

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