如何从布局Html.Partial呼叫控制器 [英] How To Call Controller from Layout Html.Partial
问题描述
我和Asp.net MVC 4,发动机工作剃须刀。
我正在开发一个应用程序,菜单是动态生成用户的角色创建
I'm working with Asp.net MVC 4, engine razor. I am developing an application and the menu is dynamically created by the user's role
更新
在我的应用程序需要用户与用户角色,这些恢复的数据输入凭据,并建立角色的动态菜单。
我的主屏幕是:
UPDATE In my application I need the user to enter their credentials with these recovered data on user role and build the dynamic menu for the role. My main screen is:
在我的_Layout.cshtml我有这个code:
in my _Layout.cshtml I have this code:
@if (Request.IsAuthenticated){
Html.Partial("Menu");
} else {
<div class="sectiontitle"> </div>
}
我创建了一个基本的控制器:
I create a base controller:
public class BaseController : Controller
{
public BaseController(){
ViewBag.Menu = BuildMenu();
}
private IList<Models.AdmMenu> BuildMenu()
{
IList<Models.AdmMenu> mmList = new List<Models.AdmMenu>(){
new Models.AdmMenu(){ Id = 1, Name = "Home", ParentId = 0, SortOrder = 1} ,
new Models.AdmMenu(){ Id = 2, Name = "Admin", ParentId = 0, SortOrder = 1},
new Models.AdmMenu(){ Id = 3, Name = "Account", ParentId = 0, SortOrder = 1},
.....
.....
在我的控制器raLoginController.cs我有这样的:
And in my controller raLoginController.cs I have this:
public class raLoginController : BaseController{
[HttpPost]
public ActionResult Login(Models.AdmLogin login){
if (ModelState.IsValid){
Servicio.cSeReclamo Servicio = new Servicio.cSeReclamo(login.UserName, login.Password);
if (Servicio.ValidarUsuario()){
string Mensaje = "";
Models.AdmUsuario oAdmUsuario = new Models.AdmUsuario();
oAdmUsuario.Au_codusuario = login.UserName;
Servicio.RetornaEntidad<Models.AdmUsuario>(ref Mensaje, "admsis.adm_usuario", oAdmUsuario.getPk(), oAdmUsuario);
FormsAuthentication.SetAuthCookie(login.UserName, false);
var ticket = new FormsAuthenticationTicket(1,
login.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
LoadData(oAdmUsuario,Servicio.Pais(ref Mensaje))
);
string encTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
if (ticket.IsPersistent) {
faCookie.Expires = ticket.Expiration;
}
Response.Cookies.Add(faCookie);
return RedirectToAction("index", "raMainReclamo", new { area = "Reclamos"});
}
}
return View(login);
}
在Menu.cshtml(部分图)
In Menu.cshtml (Partial View)
@{
List<Crd.Web.Models.AdmMenu> menuList = ViewBag.Menu;
}
<div class="sectiontitle">
<ul class="menu" id="header">
@foreach (var mp in menuList.Where(p => p.ParentId == 0))
{
<li><a href="#">@mp.Name</a>
@if (menuList.Count(p => p.ParentId == mp.Id) > 0)
{
@:<ul>
}
@RenderMenuItem(menuList, mp)
@if (menuList.Count(p => p.ParentId == mp.Id) > 0)
{
@:</ul>
}
</li>
}
</ul>
</div>
但是,当我进入凭证都具有以下错误:
But when I enter the credentials have the following error:
菜单列表等于空,为什么?我下面一步的code步骤,进入BaseController和执行BuildMenu方法......为什么无效?
我有这样的消息:
menulist is equal a null, why? I am following the code step by step, enters BaseController and execute BuildMenu method... why null? I have this message:
如何解决这个问题呢?
推荐答案
您可以通过调用一个控制器动作
You can call a controller action by using
@Html.Action("Action", "Controller")
您控制器动作可以做
Session["Menu"] == null
检查并在必要时加载它。然后返回部分以
check and load it if necessary. Then return a partial with
return PartialView("_Partial");
有关使用这种方法的好处是,它可以让你获得会话[菜单]检查出你的页面布局中的code。
The nice thing about using this approach is it will allow you to get the code of checking for Session["Menu"] out of your layout page.
我不能完全肯定这是否会有所帮助;请进一步明确您的问题,如果必要的。
I'm not exactly sure if this will help; please clarify your question further, if necessary.
这篇关于如何从布局Html.Partial呼叫控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!