none
MVC3 model null issue RRS feed

  • Question

  • Hi All,

    I'm getting started with  MVC with the help of MVCmusicstore app demo. But got suck with an issue.

    My Controller                                                                                                                      Public ActionResult Details(string id)

    {
    var Album = new Album { MyTitle = "Album" };
    return View();

    }

    Model:                                                                                                                                          
      public class Genre
        {
            public string MyName { get; set; }
        }

        public class Album
        {
            public string MyTitle { get; set; }
            public Genre Genre { get; set; }
        }                                                                                                                                                   View :

    @model MvcMusicStore.Models.Album

    @{
        ViewBag.Title = "Details";
    }

    <h2>Album :@Model.MyTitle</h2>                                                                                              When i run the application , its throwing an error "object refference not set to an instance of an object" at  <h2>Album :@Model.MyTitle</h2>    . The model seems to be empty here . Could someone advise what i'm doing wrong here please



    • Edited by Jerry321 Thursday, May 23, 2013 8:47 AM
    Thursday, May 23, 2013 8:45 AM

Answers

  • You need to pass the model to the view in the controller:

    var Album = new Album { MyTitle = "Album" };
    return View(Album);

    • Marked as answer by Jerry321 Thursday, May 23, 2013 11:00 AM
    Thursday, May 23, 2013 9:52 AM

All replies