谷歌搜索了一段时间后,我对如何在asp mvc 3中这样做有点困惑.
因此,任务是为几个控制器的视图提供一个公共布局(或主?)页面.所有视图都是强类型的...这个布局页面实际上可视化一些强类型的对象.所以,我需要将这个对象传递给布局页面,将它传递给`Html.RenderPartial()'或者在页面中正确呈现它.
文章将数据传递给查看母版页(C#)(虽然是mvc2的旧版本)提供了一个示例来提供基本抽象控制器类,并将模型传递给ViewData.这可能是治愈方法.
但...
在某些线程中,我找到了一种可以创建强类型母版页的方法:
<%@ Master ... Inherits="System.Web.Mvc.ViewMasterPage<MyWeb.Models.Client>" %>
Run Code Online (Sandbox Code Playgroud)
那么,mvc3中有相同的方法吗?或者将数据传递到布局页面的唯一方法是使用ViewBag还是ViewData?
在阅读了什么是在asp.net-mvc中将ViewModel数据绑定到母版页的最佳方法之后,我已经考虑了Darin的答案,但仍然认为这一组视图将具有相同的布局,并且没有理由反对DRY原理.
那么,如何将强类型对象传递给布局页面?或者我混淆了什么?
我有这个代码
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{
// some lines of code . bla bla bla
TempData["loginModel"] = loginModel;
return RedirectToAction("index", "premium");
}
...
}
Run Code Online (Sandbox Code Playgroud)
这个控制器在这里
public ActionResult Index()
{
var loginModel = TempData["loginModel"] as LoginModel;
...
}
Run Code Online (Sandbox Code Playgroud)
现在,当页面加载时,一切似乎都运行正常.但是当我刷新时,一切都搞砸了,它说loginModel就像是null.问题是,我怎么能跟踪当前的登录用户.我启用了表单身份验证.TNX
错误如下
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it …Run Code Online (Sandbox Code Playgroud)