Shawn
Shawn

Reputation: 2435

Render a partial view with single model from a partial view using an IEnumerable

I am trying to figure out how to render a single model from a view that is using an IEnumerable of that model. I can't seem to figure out how to send it using the razor

Right now I am getting the error:

MyApp.Models.DefectsVM' is a 'type' but is being used like a 'variable'

On this line in my main view(Under DefectsVM model I am trying to pass in):

@{Html.RenderPartial("~/Views/Defect/defectsPartial.cshtml", DefectsVM);}

My partial view has this in it to use a model:

@model MyApp.Models.DefectsVM

And my main view is using this:

@model IEnumerable<MyApp.Models.DefectsVM>

I am not sure what all other information is needed, let me know if I need to edit. But thank you for reading and taking your time to help.

Upvotes: 0

Views: 764

Answers (1)

Jerad Rose
Jerad Rose

Reputation: 15513

You need to refer to your model with Model. But in your case, Model will return an IEnumerable<MyApp.Models.DefectsVM>, so you will need to iterate over that, and then render your partial.

Something like:

foreach(var defectsVM in Model)
{
    @{Html.RenderPartial("~/Views/Defect/defectsPartial.cshtml", defectsVM);}
}

Regardless of whether the IEnumerable contains one or more than one, it should work.

Upvotes: 1

Related Questions