我正在開發一個MVC 4網站。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")
我怎樣才能解決這個問題?
你能否提供一些更多的信息,如視圖代碼?你能否告訴我們該方法屬於哪個控制器? –
也是在視圖上返回部分或全部的動作? – Jared