1

I'm trying to employ a technique that I came across that seems quite clean.

Previously, my Partial had the loop inside of it. I was looping through the mode within the Partial... but then I came across an example where the foreach loop existed in the main page, while the partial was just the meat of the loop.

They accomplished it like so:

<% int index = 1; // iteration
foreach (var item in Model.Deal) { %>

     <% Html.RenderPartial("DealList", item, new ViewDataDictionary {{ "index", index }}); %>

<% i++; // increase the interation
} %>

But in my example, I'm using a ViewModel, and now that I'm in the partial, I can't access "item" like I used to be able to. Instead my only option is Model.Deal ...

What is the point of passsing "item" with the RenderParial helper if I can't access it by saying item.StoreName? Note, both the View and the Partial are strongly typed to the same ViewDataModel.

2 Answers 2

2

Inside of the partial "DealList" your model will be whatever item is in the main view. Inside of the partial view, Model.Deal refers to a Deal object inside of item (from the main view).

This means that your StoreName property will be accessible as Model.StoreName within your partial view.

As a side note, I put together an extension method to deal with rendering of multiple partial views so as to not require the looping.

The new method is called RenderPartials:

public static void RenderPartials(this HtmlHelper helper, string partialViewName, IEnumerable models, string htmlFormat)
        {
            foreach (var view in models.Select(model =&gt; helper.Partial(partialViewName,model)))
            {
                helper.ViewContext.HttpContext.Response.Output.Write(htmlFormat, view.ToHtmlString());
            }
        }

Using this method you can simple say:

<% Html.RenderPartials("DealList",Model.Deal); %>

inside your main view, without the loop.

There's some more information about this here which explains more about the htmlFormat parameter etc.

Hope this is helpful to you.

Sign up to request clarification or add additional context in comments.

10 Comments

Well, that's what I though the logic would be... but when using Model., I just get "Deal" in the intellisense and none of the following parameters... it's as if I'm doing Model.Deal[i].StoreName ... should my Partial be strongly typed to the same ViewModel or something else?
Your partial view should be strongly typed and accept a type that's the same as item. What is Deal a collection of?
My main view is strongly styped to BLL.ViewModels.DealViewModel, which contains "Deal" ... should that also be what my Partial is strongly typed to? Or just <Deal>?
Your partial view should be strongly typed to whatever "item" is in your loop. For example, lets say Model.Deal is an IEnumerable<DealItem>, which makes 'item' a 'DealItem', then your partial view should be strongly typed to the type 'DealItem'.
It's List. So if I calling RenderPartial("DealList", item);, my Partial should be strongly typed to <List<Deal>>?
|
0
@model IEnumerable<dynamic>

@foreach (dynamic m in Model)
{
     @Html.Partial(MVC.Lists.Messages.Views._SingleMessage, (object)m)
}

1 Comment

You may want to explain your answer a little more... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.