So it sounds like you want to display the items from a collection within your view, by looping through them? You'd be better off using editor templates rather than partial views. MVC has some nice features within display and editor templates for handling collections, which you can take advantage of. I've written an article on using Display Templates, which you might find useful.
I can't tell whether you have any form inputs within your partials, so can't advise whether to use editor or display templates. The rule of thumb is that if you're displaying read-only markup, use Display templates. If you're providing form controls for user interaction, use Editor Templates. You've called yours an Editor, so we'll go with that.
I would separate the business of displaying the markup from the business of collating the data. So your controller only concerns itself with creating the model and not which template to render. We'll leave that consideration to the view. Add a ViewModel for the page, which you'll add your office holders to. Let's call it OfficeHoldersTestViewModel:
public class OfficeHoldersTestViewModel
{
public IEnumerable<OfficeHolder> OfficeHolders { get; set; }
}
Your Controller action now only needs to create one of these and populate it with the office holders:
public ActionResult office_holders_Test()
{
int Application_id = Convert.ToInt32(TempData["Application_id"]);
var model = new OfficeHoldersTestViewModel { OfficeHolders = new List<OfficeHolder>() };
if (Application_id != 0)
{
var office_holders = Applicant_Service.GetOfficeHolderBy_Application_Id(Application_id);
model.OfficeHolders = office_holders;
}
return View(model);
}
Next, create an Editor Template in Views/Shared/EditorTemplates called OfficeHolder.cshtml and set the model property to your OfficeHolder class:
@model OfficeHolder
<!-- markup for an Office Holder goes here -->
Finally, within your main view, set the model to the OfficeHoldersTestViewModel and use the EditorFor extension method where you want your office holders to appear:
@model OfficeHoldersTestViewModel
<div class="row">
<div class="col-xs-12">
@Html.EditorFor(m => m.OfficeHolders)
</div>
</div>
The nice thing about this extension method is that it works for collections as well as individual objects. In this case, it will use the template you've created for your single Office Holder and apply it to each item in the collection, rendering them one below the other.
Further reading on using Display Templates, which you might find useful.
return PartialView(...);You method can only return 1 view - you need to pass the collection to the view and use a foreach loop in the view to generate the html for each item - user3559349