ASP.Net MVC 3发送字符串从HTTP-POST方法_Layout.cshtml [英] ASP.Net MVC 3 Sending string from HTTP-POST Method to _Layout.cshtml

查看:256
本文介绍了ASP.Net MVC 3发送字符串从HTTP-POST方法_Layout.cshtml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,这样它使用我自己的后台数据库,以适应AccountController类。为了做到这一点我完全需要发送一个字符串_Layout.cshtml进行检查。这怎么可能?

I am trying to adapt the AccountController class so that it uses my own backend database. In order to do this completely I need to send a string to the _Layout.cshtml to be checked. How is this possible?

我曾尝试与ViewData的,但是这需要控制器对应例如连接视图的AccountController查看/帐号/ LogOn.cshtml会工作。

I have tried with ViewData but this requires the Controller to correspond to the view attached e.g. AccountController to View/Account/LogOn.cshtml will work.

我相信ViewBag工作在相同的方式,我得到一个空引用,当我尝试访问它在_Layout.cshtml。

I believe that ViewBag works in the same way as I am getting a null reference when I try to access it in the _Layout.cshtml.

目前我的code是因为要试图解决这一问题,但在这里受损是我。希望这将有助于更好地解释。

At the moment my code is a bit damaged due to trying to fix the problem but here is what I have. Hopefully it will help to explain better.

的AccountController / [HTTP-POST] LogOn支持

AccountController/[HTTP-POST] LogOn

...
if (user.GetRole(model.UserName).Equals("Admin"))
{
     ViewBag.Role = "Admin";
}
...

_Layout.cshtml

_Layout.cshtml

@if (Request.IsAuthenticated && ViewBag.Role.Equals("Admin"))
{
   ...
}

我不再觉得这可以ViewBag或ViewData的(由于评论)完成。任何解决方案将受到欢迎。

I no longer think this can be done with ViewBag or ViewData (Due to comments). Any solution would be welcome.

感谢您提前 - 安口

推荐答案

更改code到

if (user.GetRole(model.UserName).Equals("Admin"))
{
    ViewBag.Role = "Admin";
}
else{
    ViewBag.Role = "";
}

您将获得 ViewBag.Role的误差不存在。因此,必须始终设置。

You will get an error of ViewBag.Role does not exist. So it must always be set.

编辑

从您的意见,我想你可能是最好创建它有它自己的控制器,并为你工作一个子操作。

From your comments, I think you might be best creating a Child Action which has it's own controller and does the work for you.

@Html.RenderAction("LogonDisplay");

这将检查角色,设置ViewBag值,然后显示为需要的。

This will check the Roles, set the ViewBag values, and then display as needed.

这篇关于ASP.Net MVC 3发送字符串从HTTP-POST方法_Layout.cshtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆