【发布时间】:2012-01-15 02:46:24
【问题描述】:
我有一个 _layout 页面,它有一个登录框(部分视图),并且该视图有它自己的模型。所以控制器看起来像这样:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if(ModelState.IsValid)
{
var g = new GallaryImage();
var user = g.LoginUser(loginModel.Username, loginModel.Password);
if(user != null)
{
FormsAuthentication.SetAuthCookie(user.username, false);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Invalid Username/Password");
}
return View(loginModel);
}
但只要我的主要内容页面需要模型,我的 Web 应用程序就会失败,因为登录框需要 LoginModel 类型,但我的内容页面正在发送不同的模型:
这是我的主索引屏幕的 GET 方法:
public ActionResult Index()
{
IndexModel model = new IndexModel();
var g = new GallaryService.GallaryImage();
var i = g.GetRandomImage();
if (i != null)
model.RandomImageUrl = "~/Images/Watermarks/" + i.filename;
return View(model);
}
所以,我的主要内容页面有一个 IndexModel,但我的部分视图有一个 LoginModel。当我尝试运行它时,我得到一个错误:
“传入字典的模型项的类型为‘GalleryPresentation.Models.IndexModel’,但此字典需要‘GalleryPresentation.Models.LoginModel’类型的模型项。”
我该如何处理 - 我的 _layout 需要登录框的模型。
根据要求,这里是登录框 cshtml 文件。
@using GalleryPresentation.Models
@model LoginModel
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
@using (Html.BeginForm("index", "Account", FormMethod.Post))
{
<table class="smallBox">
<tr>
<td>@Html.LabelFor(m => m.Username)</td>
<td>@Html.TextBoxFor(m => m.Username, new { @class = "smallText" })</td>
<td>@Html.LabelFor(m => m.Password)</td>
<td>@Html.PasswordFor(m => m.Password, new { @class = "smallText" })</td>
</tr>
<tr>
<td colspan="4" align="right"><input type="submit" value="Login"/></td>
</tr>
<tr>
<td colspan="2">@Html.ValidationSummary()</td>
</tr>
</table>
}
Index.cshtml 文件(主内容屏幕)有这个:
@using GalleryPresentation.Models
@model IndexModel
@{
ViewBag.Title = "Craig and Melanie's Digital Moments";
}
<br/>
<div style="text-align: center">
<img src="@Url.Content( Model.RandomImageUrl)" alt="@ViewBag.Title" />
</div>
【问题讨论】:
-
请包含登录框部分视图cshtml。
-
看起来我可以使用我的 LoginModel 作为所有其他模型的基类,然后继承它?这似乎可行 - 但这是好的设计/实践吗?我需要确保所有模型都从 LoginModel 继承(我想我会重命名为 BaseModel - 以防需要其他东西?)。
-
这是一个选项,但我不建议这样做。将 LoginModel 视为基本模型意味着您的每个视图模型都是 LoginModel 的扩展。
标签: asp.net-mvc asp.net-mvc-3 razor