MVC 4多模型绑定

时间:2012-11-28 20:16:05

标签: c# asp.net-mvc

我正在开发一个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")

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

您需要在_layout.cshtml视图中指定要传递给登录视图的模型...

示例:

@Html.Partial("_yourloginview", new LoginModel())

答案 1 :(得分:0)

传递给视图的数据类型应与您在视图中声明的模型相对应。例如:

public ActionResult SomeDerp()
{
    Derp derp = new Derp();
    return View(derp);
}

然后视图应该在SomeDerp页面上声明一个模型,可能是这样的:

@model Derp

很抱歉,如果我误解了你的问题:)

答案 2 :(得分:0)

原因是因为在你的_layout.cshtml中你指定了Logon模型,所以现在每个页面都需要这个模型。

相关问题