Placek
Placek

Reputation: 699

MVC Multiple Models

I have problem with creating multiple model view

I've created 3 models and then one multiple model:

public partial class DetailsModel
{
    public ANIME MyANIME { get; set; }
    public CHARACTER MyCHARACTER { get; set; }
    public ACTORS MyACTORS { get; set; }
}

After that I added lines to Controller:

    public ActionResult Details(int id)
    {
        DetailsModel myDetailsModel = new DetailsModel();
            return View(myDetailModel);
    }

And after that View:

<div class="editor-label">
    @Html.LabelFor(model => model.MyANIME.TITLE_OR)
</div>
<div>
    @Html.DisplayFor(model => model.MyANIME.TITLE_OR)
</div>

But when I start it the data is always empty... Any ideas what is wrong?

Upvotes: 0

Views: 104

Answers (2)

CodeCaster
CodeCaster

Reputation: 151730

I assume the classes ANIME, CHARACTER and ACTORS are entities used by your Entity Framework context. And please use proper casing, pascal case in this case.

You'll have to populate the ViewModel with the proper Anime, Character and Actors. Still assuming Entity Framework, your controller will look something like this:

public ActionResult Details(int id)
{
    using (var dbContext = new YourContext())
    {
        DetailsModel viewModel = new DetailsModel();

        viewModel.Anime = dbContext.Anime.FirstOrDefault(a => a.ID == id);
        viewModel.Character = dbContext.Characters.FirstOrDefault(a => a.ID == id);
        viewModel.Actors = dbContext.Actors.FirstOrDefault(a => a.ID == id);

        return View(viewModel);
    }
}

Of course you'd want to alter the query for each entity, you probably don't want to use entities in your view and the datasource should be injected in the controller's constructor, but I guess something like this is what you're after.

Upvotes: 0

David
David

Reputation: 219097

This creates an instance of your model:

DetailsModel myDetailsModel = new DetailsModel();

But what does that instance consist of? For starters, the three properties inside of that model are all of reference types, so they're all going to default to null anyway. Thus, within the view, model.MyANIME is null. You can fix that small part by always initializing the properties (for example, in the default constructor):

public partial class DetailsModel
{
    public ANIME MyANIME { get; set; }
    public CHARACTER MyCHARACTER { get; set; }
    public ACTORS MyACTORS { get; set; }

    public DetailsModel()
    {
        MyANIME = new ANIME();
        MyCHARACTER = new CHARACTER();
        MyACTORS = new ACTORS();
    }
}

But this doesn't actually populate them with meaningful data either. If TITLE_OR is a string, for example, then even in this case model.MyANIME.TITLE_OR is still going to be null (or, at best, an empty string).

Whatever data you want to show on that page, you need to get that data from somewhere and populate the model with that data. Then it will be available on the page. For example, if you do this in your controller then you'll see the example value on the page:

DetailsModel myDetailsModel = new DetailsModel();
myDetailsModel.MyANIME = new ANIME();
myDetailsModel.MyANIME.TITLE_OR = "sample title";
return View(myDetailsModel);

Upvotes: 2

Related Questions