0

I have a view page with multiple models and i am having trouble getting the data to display but i could save any added data just fine. It is just displaying the data on page load tells me that the model i am passing in passes a null reference.

Main view:

@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>

My models:

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

Controller - For each model i have a get and post method except the PlayName just has a GET.

UserName Controller:

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) shows Object reference not set to an instance of an object. and i am not sure how to properly fix this.

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

Post for Username:

[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");
    }

Username Page:

@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>
}
BB987
  • 181
  • 3
  • 14
  • you need to include the action method for your main view. without it, there's no way to tell how the UsernameModel was populated – Dave Alperovich Feb 12 '13 at 04:08
  • @DaveA i went ahead and added the main view action method - how can i properly add the information to pass in the information for other models – BB987 Feb 12 '13 at 04:19
  • I don't see any model being passed. I would expect return(model) with model being an object of type ProfileModel – Dave Alperovich Feb 12 '13 at 04:22
  • @DaveA so in the `Profile` action method - i would add `var profileModel = new ProfileModel();` and in the return - `return View(profileModel)` ? – BB987 Feb 12 '13 at 04:27
  • yes, correct. But, then you'll need to instantiate the member classes as well or they will come up as null refs when u pass them to your partials – Dave Alperovich Feb 12 '13 at 04:29
  • @Karthik, I think BB987 meant to use _UsernamePartial as a partial with a child action. – Dave Alperovich Feb 12 '13 at 04:31
  • @DaveA I have each of the username, playname and password models instantiated - i only should the class calling all them. i just didn't want to scare away great helpers with tons of code. I will try ur suggestion now and will be back. Thanks! – BB987 Feb 12 '13 at 04:32
  • @DaveA Yes you are right. – Karthik Chintala Feb 12 '13 at 04:34
  • @DaveA I tried and now the error message has changed: `The model item passed into the dictionary is of type 'Project.Models.ProfileModel', but this dictionary requires a model item of type 'Project.Models.UsernameModel'.` for this line on my Profile view page `@Html.Partial("_UserNamePartial", Model.Username)` I feel like i'm looping myself. Would you happen to know good tutorials that will help clear my thinking structure. – BB987 Feb 12 '13 at 04:44
  • The message tells you that your partial expects a model of type Username, but is getting a type ProfileModel. This confuses me. You clearly are passing the UserName Model to your partial. -- many good tutorials, but a book is better – Dave Alperovich Feb 12 '13 at 04:47
  • please add the _UsernamePartial to your POST – Dave Alperovich Feb 12 '13 at 04:52
  • @DaveA - i justed added code for my POST but i am still getting the same previous error. – BB987 Feb 12 '13 at 05:02
  • thats your action method, but what about the body of _UserNamePartial? at the top what kind of model is it bound to? @model usernameModel? – Dave Alperovich Feb 12 '13 at 05:07
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Feb 12 '13 at 05:11
  • @DaveA Just added code above – BB987 Feb 12 '13 at 05:12
  • @JohnSaunders, in this long, very unsactioned thread we got BB987 past the null ref. now we have a major mystery. he is passing UsernameModel to his _UserNamePartial, which it expects, but gets an error that he is passing the parent model profileModel instead. I'm at a loss. other than that his Partial seems to reference another Project, I see no smoking gun here. – Dave Alperovich Feb 12 '13 at 05:15
  • If the question has changed, then ask a separate question. This is not a thread, and this is not a forum. – John Saunders Feb 12 '13 at 05:17
  • @JohnSaunders, that's fair. BB987, you should repost this question. probly best to delete this one and ask why you are getting a type error for your partial when it seems to be sent correctly. – Dave Alperovich Feb 12 '13 at 05:18

2 Answers2

0

Your Null Ref was the result of View() Factory call without the bound model.

Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101
0

Use this code in Class

public virtual PasswordModel PasswordModel1 
        {
            get {

                PasswordModel PasswordModel2 = this.PasswordModel.FirstOrDefault();

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

                }

                return PasswordModel2;
            }
        }

use this code in view

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

Do this For Other Model as well Hope It will help u.

hungry
  • 37
  • 1
  • 13