0

我有一个包含多个模型的视图页面,我无法显示数据,但我可以很好地保存任何添加的数据。它只是在页面加载时显示数据告诉我我传入的模型传递了一个空引用。

主视图:

@model Project.Models.ProfileModel
@{
    ViewBag.Title = "Profile";
}

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


<div id="wrap">
    <div id ="right">
        @(ViewBag.HasLocalPassword ? Html.Partial("_ChangePasswordPartial", Model.PasswordModel) : Html.Partial("_SetPasswordPartial", Model.PasswordModel))

    </div>
    <div id="left">
            @Html.Partial("_UsernamePartial", Model.UsernameModel)
            @Html.Partial("_PlayNamePartial", Model.PlayNameModel)

    </div>

</div>

我的模型:

 public class ProfileModel
{
    public PasswordModel PasswordModel { get; set; }
    public PlayNameModel PlayNameModel { get; set; }
    public UsernameModel UsernameModel { get; set; }
}

控制器 - 对于每个模型,我都有一个 get 和 post 方法,除了PlayName只有一个 GET。

用户名控制器:

public ActionResult _UsernamePartial()
    {
        var usernameModel = new UsernameModel();
        using (var db = new DataContext())
        {
            usernameModel.Username =
                (from u in db.Users where u.ID == WebSecurity.CurrentUserId select u.Username).FirstOrDefault();
        }
        return PartialView(usernameModel);
    }

@Html.Partial("_UsernamePartial", Model.UsernameModel)显示Object reference not set to an instance of an object.,我不确定如何正确解决此问题。

public ActionResult Profile(ManageMessageId? message)
    {
        ViewBag.StatusMessage =
            message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
            : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
            : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
            : "";
        ViewBag.HasLocalPassword = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
        ViewBag.ReturnUrl = Url.Action("Profile");
        return View();
    }

用户名发帖:

[HttpPost]
    public ActionResult _UsernamePartial(UsernameModel usernameModel)
    {
        if (ModelState.IsValid)
        {
            using (var db = new DataContext())
            {
                User user = db.Users.FirstOrDefault(u => u.ID == WebSecurity.CurrentUserId);
                user.Username = usernameModel.Username;
                db.SaveChanges();
            }
        }
        return View("_UsernamePartial");
    }

用户名页面:

@model Acatar.Models.UsernameModel

@using (Html.BeginForm("_UsernamePartial", "Account")) {

       <p id="legend">Username</p>
       @Html.TextBoxFor(m=>m.Username)

    <button type="submit" value=" Username">Save</button>
}
4

2 回答 2

0

您的 Null Ref 是没有绑定模型的 View() Factory 调用的结果。

于 2013-02-12T05:28:30.270 回答
0

在类中使用此代码

public virtual PasswordModel PasswordModel1 
        {
            get {

                PasswordModel PasswordModel2 = this.PasswordModel.FirstOrDefault();

                if (PasswordModel2 == null)
                {
                    PasswordModel2 = new PasswordModel();

                }

                return PasswordModel2;
            }
        }

在视图中使用此代码

@Html.Partial("_UsernamePartial", Model.UsernameModel, new ViewDataDictionary(Html.ViewDataContainer.ViewData)
 {
     TemplateInfo = new System.Web.Mvc.TemplateInfo { }
 })  

对其他模型也这样做希望它会帮助你。

于 2013-02-12T05:32:32.943 回答