在部分视图中访问数据库信息,.ascx 包含在 asp.net mvc 的 Site.Master 中
嗨,我遇到了这个问题。我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仍然可以在控制器中执行此操作。将相关数据填充到 ViewData 字典中:
在母版页中使用:
您可以通过创建为您执行此操作的自定义属性或将其放入所有控制器扩展的基类。
或者,您可以只将菜单项填充为模型的一部分,并将模型传递给 PartialView。
You can still do this in your controller. Populate your the relevant data into the ViewData dictionary:
In your master page use:
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.