我正在开发一个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.”,但该字典需要一个“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 im呈现登录表单中
@Html.Partial("_LoginPartial")我怎样才能解决这个问题?
发布于 2012-11-28 20:36:03
您需要指定要传递给_layout.cshtml视图中的登录视图的模型.
示例:
@Html.Partial("_yourloginview", new LoginModel())发布于 2012-11-28 20:25:11
传递给视图的数据类型应该与视图中声明的模型相对应。例如,像这样:
public ActionResult SomeDerp()
{
Derp derp = new Derp();
return View(derp);
}然后视图应该在SomeDerp页面的顶部声明一个模型,可能如下所示:
@model Derp对不起,如果我误解了你的问题:)
发布于 2014-01-20 03:29:37
这是因为在您的_layout.cshtml中您指定了登录模型,所以现在每个页面都想要这个模型。
https://stackoverflow.com/questions/13613638
复制相似问题