具有不同模型的局部视图,空参考误差

时间:2013-06-17 17:33:18

标签: asp.net-mvc razor

我正在编写一个注册页面,其中包含注册和登录的两个选项。我希望这些视图和模型保持独立,因此我使用的是部分视图。但是,当第二个部分视图尝试初始化其模型时,我得到一个空引用异常。帮助将不胜感激。

空引用异常发生在

@Html.Partial("Login",Model.Login)

RegisterModel

public class RegisterModel
{
    public LoginModel Login { get; set; }

    public RegisterModel()
    {
        Login = new LoginModel();
    }

    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

LoginModel

public class LoginModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }

Login.cshtml

    @model MvcApplication1.Models.LoginModel

@{
    ViewBag.Title = "Log in";
}

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
</hgroup>

<section id="loginForm">

@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 class="social" id="socialLoginForm">
    <h2>Use another service to log in.</h2>
    @Html.Action("ExternalLoginsList", new { ReturnUrl = ViewBag.ReturnUrl })
</section>*@

和Register.cshtml(索引)

    @model MvcApplication1.Models.RegisterModel
@{
    ViewBag.Title = "stuff";
}

@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <div class="register">
                <div class="registration_contents">
                    @Html.Partial("RegisterForm")

                </div>
                <div class="login_contents">

                    @Html.Partial("Login",Model.Login)

                </div>
            </div>
        </div>        
    </section>
}      



@section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        }

1 个答案:

答案 0 :(得分:1)

下午好,看起来在您对Register视图的get请求中,您很可能不会直接实例化您的RegisterModel,所以当您将Model.Login传递给部分调用时它是null。