【问题标题】:How to pass Viewdata value to masterpage in Razor MVC4如何在 Razor MVC 4 中将 Viewdata 值传递给母版页
【发布时间】:2012-10-30 11:59:35
【问题描述】:

我使用目录服务登录我的页面。我需要将用户名传递到我的母版页以在所有页面中显示用户名。

我获得了用户名并将其存储在 ViewData 中。如何在 masterpage 中传递 viewdata 值。
我的代码:

 [HttpPost]
    public ActionResult Index(LoginModels model)
    {
        if (ModelState.IsValid)
        {
            string DisplayUserName = string.Empty;
            string LoginUser = model.Userid;
            string LoginPassword = model.Password;    
            string  name = model.UserName
            if (ValidateActiveDirectoryLogin(LoginUser, LoginPassword, out DisplayUserName) == true)
            {
                model.UserName = DisplayUserName;
                ViewData["UserName"] = "Welcome" + DisplayUserName;
                return RedirectToAction("Index", "MPP", new { UserID = LoginUser });
            }          
            else
            {
                ModelState.AddModelError("","Invalid Username or Password");
            }               
        }
        return View();          
    }

在布局页面中:

   @{  @ViewData["UserName"]   }


我尝试了以下方式来显示用户名。但它会引发 nullexception。
编辑:

@foreach (var m in IEnumerable<SampleECommerce.Models.LoginModels>)ViewData["UserName"])
{ 
    @m.UserName
} 

【问题讨论】:

    标签: c# asp.net asp.net-mvc-3 master-pages


    【解决方案1】:

    存在一些误解,例如如果将ViewData["UserName"] 设置为字符串值,则会得到IEnumerable&lt;SampleECommerce.Models.LoginModels&gt;。这是另一个解决方案:

    把这个放到布局页面:

    <span>@{Html.RenderAction("actionname", "controllername");}</span>
    

    在相关行动中:

     public ActionResult actionname() {
            string result = getusername();
            return Content(result);
        }
    
    
    [NoneAction]
    private string getusername(){
        return (Membership.GetUser()!= null) ? Membership.GetUser().UserName : "Guest";
    }
    

    【讨论】:

      【解决方案2】:

      试试不用额外的@,即

         @{  ViewData["UserName"]   }
      

      【讨论】:

      • 虽然不需要,但它仍然可以与额外的@一起使用
      【解决方案3】:

      首先您需要将语法更改为:

      @(ViewData["UserName"])
      

      这可能是最好的(一堆坏的)。实际上,您应该寻求通过控制器的 User 属性(通常在您读取 cookie 的授权属性中)将您的用户推入页面的 User 属性 - 这样您就不会依赖关于类型不安全的ViewData 和魔法字符串,用于您将在每个页面上使用的东西。

      但是无论如何...如果由于最后一行 return View(); 导致视图正在呈现,那么如果您按照我所示更改语法,那么您尝试执行的操作将起作用。

      如果不是,那么当您执行return RedirectToAction("Index", "MPP", new { UserID = LoginUser }); 时,您需要将用户名推送到TempData,然后在您的Index 控制器上的Index 操作开始时将其读回:

      所以:

      TempData["UserName"] = "Welcome " + DisplayUserName;
      return RedirectToAction("Index", "MPP", new { UserID = LoginUser });
      

      然后在您的Index 方法开始时,您需要从TempData 中提取值:

      public class MPPController {
        public ActionResult Index(){
          ViewData["UserName"] = TempData["UserName"];
        }
      }
      

      为什么你必须这样做?因为RedirectToAction 不呈现页面——它告诉客户端向新的Url 发出不同的请求——因此就服务器而言,任何ViewData 或模型或任何东西都会被丢弃。 TempData 用于在两个连续请求之间提供临时存储 - 因此它适用于 RedirectToAction 场景。

      就像我说的那样 - 这确实是一种将您的用户信息从控制器保存到视图的糟糕方法,您应该认真地重新考虑它作为紧急事项。

      【讨论】:

        【解决方案4】:

        在布局页面中:

        <span>@{Html.RenderAction("actionname", "controllername");}</span>
        

        在控制器中存储一个会话变量

         [HttpPost]
        public ActionResult Index(LoginModels model)
        {
              Session["username"] = model.UserName;
             //remaining code
        }
        

        增加一个功能

        public ActionResult actionname() {
        
            return Content(Session["username"]);
        }
        

        所以这里我们不需要额外的功能。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-22
          • 2012-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多