ログインボックス(部分表示)を持つ_レイアウトページがあり、そのビューには独自のモデルがあります。だから、コントローラは次のようになります。MVC3複数モデル - シングルページ
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);
}
しかし、すぐに私のメインのコンテンツページログインボックスがLoginModelタイプを想定しているため、私のWebアプリケーションに障害が発生し、モデルを必要としますが、私のコンテンツページが別のモデルを送っているよう:
これは私のメインのインデックス画面のための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にはログインボックスのモデルが必要です。
リクエストに応じて、ここにはLoginboxの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。 –
@Phil - コードを追加しました。 – Craig
他のすべてのモデルのベースクラスとしてLoginModelを使用して継承できるようですか?それはうまくいくようですが、それは良いデザイン/実践ですか?私はすべてのモデルがLoginModelから継承していることを保証する必要があります(これはBaseModelに名前を変更すると思います。 – Craig