0

I have a model, which can represent 3 categories. I want in my view, make 3 different tables for each category with relevant fields. I think for this I need to use partial view with viewmodel for each category.

So my main model is "Ad", which have 3 sub viewmodels (Realty, Auto and Service). Here the example how I implement Realty action on my home controller:

public ActionResult Realty()
    {
        var ads = db.Ads.Include(a => a.Realty);
        var vm = new List<RealtyViewModel>();

        foreach (var ad in ads)
        {
            vm.Add(new RealtyViewModel
            {
                Title = ad.Title,
                Descirpiton = ad.Descirpiton,
                Type = ad.Realty.Type,
                NumberOfRooms = ad.Realty.NumberOfRooms
            });
        }

        return PartialView(vm);
    }

Then my partial view, looks like this:

@model IEnumerable<OGAS.Areas.Category.ViewModels.RealtyViewModel>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Type)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Descirpiton)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberOfRooms)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Type)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Descirpiton)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NumberOfRooms)
            </td>
        </tr>
    }

</table>

Then in my Index page (without using any models), I call partial view like this:

@{Html.RenderPartial("Realty");}

But then I'm getting following error:

An exception of type 'System.NullReferenceException' occurred in App_Web_gdyh352c.dll but was not handled in user code

Could you please advise if this approach is good (calling 3 vms), if yes how to implement this?

Thanks.

2 Answers2

1

Try to replace @{Html.RenderPartial("Realty");} and use @Html.Action("Realty") in this case, as you need to call back to the controller action, in order to create the model for the partial view.

See MVC Html.Partial or Html.Action for more information.

Community
  • 1
  • 1
James P
  • 2,201
  • 2
  • 20
  • 30
  • Big thanks, this works. Can you please advise I have a doubt, is this approach good (from performance standpoint), I'll end up by calling 3 Html.Action methods with 3 different vm. – Азамат Исмагулов Oct 24 '16 at 06:13
  • Its quite common approach with MVC. You are still loading the same data - you will need to call it from the controller at some point. Your other option is to create 1 large view model for the main view and use e.g. `@Html.Partial("_partialName", model.Realty)` – James P Oct 24 '16 at 09:49
1

Use this, for .net core and mvc. @Html.Action has been removed from .net core

 @await Html.PartialAsync("_YourPartialViewName", YourModel)
Abdus Salam Azad
  • 5,087
  • 46
  • 35
  • How do you bind YourModel to something then? With RenderAction() you were able to call a specific controller method to populate the model. I can't understand how to do this now, in .NET Core without the action() method. – KirstieBallance Mar 25 '23 at 20:46