2

I have the following snippet of code inside one of my views:

foreach (var topEmployer in Model.TopEmployers)
{
    //Some link - works fine for all iterations
    @Html.ActionLink(....)

    //This partial view is only rendered for the LAST iteration of this loop.
    @Html.Partial("partialViewName", topEmployer)   
    <br />     
}

Stepping through the loop with a debugger where I have a breakpoint in the partial view confirms the issue: The debugger only steps into the partial view at the last iteration not on any previous. Seems like it just ignores the partial view on previous iterations.

I'm not sure how I would resolve this issue. Have tried to search for similar problems but without luck. What am I doing wrong?

EDIT: Ok, I placed the breakpoint in the partial view inside an if statement that wasn't being entered. So, that's an entirely different issue. The partial view is being rendered but as there is no content it's just empty and the breakpoint obviously never got hit.

Force444
  • 3,321
  • 9
  • 39
  • 77
  • I suggest you take a look at : http://stackoverflow.com/questions/11475308/rendering-partial-views-in-a-loop-in-mvc3 and use the editor/display templates idea mentioned in Darin's answer, see if that helps you. – Jason Evans Nov 12 '14 at 08:50
  • @JasonEvans Is there no way of making it work within a forloop? I tried the Html.RenderPartial alternative as well, but it still only shows the last Partial view. – Force444 Nov 12 '14 at 09:18

1 Answers1

2

I tried to replicate your issue, to no avail.

Here's my Home Index.cshtml

@model PartialViewForLoop.Controllers.IndexViewModel
@{
    ViewBag.Title = "Home Page";
}

@{
    foreach (var topEmployer in Model.TopEmployers)
    {
        //This partial view is only rendered for the LAST iteration of this loop.
        @Html.Partial("partialViewName", topEmployer)
        <br />
    }
}

The partial view

@model string

Hello @Model

and the controller code:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var vm = new IndexViewModel();
            vm.TopEmployers.Add("Mr Tim Jones");
            vm.TopEmployers.Add("Mr David Smith");

            return View(vm);
        }
    }

public class IndexViewModel
{
    public IndexViewModel()
    {
        TopEmployers = new List<string>();
    }

    public IList<string> TopEmployers { get; set; }
}

When I render the Index view, the for loop executes twice and for each name, the partial view is rendered. I can't see why, in your example, the partial view is being rendered only once.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • Hmm. I tried changing the partial view to only contain a simple string like in yours. In that case the partial view is now being rendered twice. So the fault lies in the partial view. I'll update my answer with the contents of the partial view , if you could have a look at that thanks. – Force444 Nov 12 '14 at 09:31
  • No probs :) Glad you found the issue. – Jason Evans Nov 12 '14 at 09:42
  • What is the exact issue? – Aravin Dec 05 '18 at 18:25
  • @Aravin From my understadning of the question EDIT (@DSF feel free to correct me), it seems that the OP had a partial view, which was empty of content, being rendered. They got the impression that the partial view was not being rendered, because nothing was showing in the browser where the partial should be. However, the partial was empty, hence the reason why it wasn't showing anything. – Jason Evans Dec 06 '18 at 11:36