0

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

空引用异常发生在

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

注册模型

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; }

登录模型

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; }

登录.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")
        }
4

1 回答 1

2

下午好,看起来在您对 Register 视图的 get 请求中,您很可能没有直接实例化 RegisterModel,因此当您将 Model.Login 传递给部分调用时,它为空。

于 2013-06-17T18:22:12.503 回答