【发布时间】:2012-11-28 20:16:04
【问题描述】:
我正在开发一个 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")
我该如何解决这个问题?
【问题讨论】:
-
能否提供更多信息,例如查看代码?您能否告诉我们该方法属于哪个控制器?
-
Actions 也是返回部分视图还是完整视图?
标签: c# asp.net-mvc