我正在开发一个 MVC 4 网站。
在我的站点中,我在部分视图中有一个登录表单,我在 _layout.cshtml 中呈现它,它使用LoginModel
.
我也有一个联系表,它使用ContactModel
当我收到联系表格并提交时,一切都很好。它去了服务器端。执行后我返回一个视图并将其绑定到ContactModel
这很简单:
[HttpPost]
public ActionResult Contact(Contact model)
{
if (ModelState.IsValid)
{
//somecode here
}
return View(model);
}
它变得越来越复杂,MVC 尝试绑定ContactModel
到登录页面并给出以下错误
传入字典的模型项的类型为“My_Project.Model.ContactModel”,但此字典需要“My_Project.Models.LoginModel”类型的模型项。
我的联系表格视图:
@model My_Project.Model.ContactModel
@{
ViewBag.Title = "Contact";
}
@using (Html.BeginForm("Contact", "Home", FormMethod.Post, new { id = "formContact" }))
{
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.Name, new {@class = "c2inp1", @placeholder = "Name"})
@Html.ValidationMessageFor(model => model.Name)
<br/>
@Html.TextBoxFor(model => model.Surname, new {@class = "c2inp2", @placeholder = "Surname"})
@Html.ValidationMessageFor(model => model.Surname)
<br/>
@Html.TextBoxFor(model => model.Email, new {@class = "c2inp2", @placeholder = "Email"})
@Html.ValidationMessageFor(model => model.Email)
<br/>
@Html.TextAreaFor(model => model.Message, new { @class = "c2inp3", @placeholder = "Message" })
@Html.ValidationMessageFor(model => model.Message)
<br/>
<input type="image" src="@Url.Content("~/Images/c2img4.png")" alt="" class="c2submit"/>
}
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我在部分视图中的登录表单
@model My_Project.Model.LoginModel
@{
ViewBag.Title = "Log in";
}
@if (WebSecurity.IsAuthenticated)
{
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
</hgroup>
<section id="loginForm">
<h2>Use a local account to log in.</h2>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<input type="submit" value="Log in" />
</fieldset>
}
</section>
</section>
}
在我的 _layout.cshtml 中即时呈现登录表单
@Html.Partial("_LoginPartial")
我该如何解决这个问题?