【问题标题】:Partial View with Different Models, Null Reference Error不同模型的局部视图,空参考误差
【发布时间】:2013-06-18 02:22:12
【问题描述】:

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

空引用异常发生在

@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")
        }

【问题讨论】:

  • 空引用出现在哪里?
  • 您能发布更多信息吗?也许是堆栈跟踪?
  • RegisterForm 局部视图在哪里?

标签: asp.net-mvc razor


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多