0

I am trying to call multiple partial views into one controller. I have a carousel that will have at least 3 slides that will be managed by a CMS. I want to be able to do a partial view for each of those slide and call them all into the home controller with an array. I know that my view will need the foreach loop but do I need a separate model for each of those partial views or will one model work? I did look at this question and answers but I think its a little different than what I am looking for (ASP.NET MVC - How to pass an Array to the view?) I've included the partial view first. The view, model and controller code. I apologize ahead of time the code is a mess. Any help would be appreciated. Thank you.

    Partial View - @{
ViewData["Title"] = "_CorporateEvents";
}

<div>
@foreach (var image in Model)
{
    <img width="850" height="700">
    <img source src="@image.EventImages.File.Url" type="image"
         alt="Your browser does not support the image tag." />
}

       View--<!-- Wrapper for slides -->
    <div class="carousel-inner">
        <div class="item active">
         </div>
        <div class="item">
        @Html.Partial("~/Views/Shared/_CorporateEvents.cshtml");
        </div>




     Model---namespace MyLink.Models
     {
public class CorporateEvents
{
    [JsonProperty("eventImages")]
    public Asset EventImages { get; set; }
}
   }





   Controller ----public async Task<IActionResult> Index()
    {

        var qb = QueryBuilder<CorporateEvents>.New.ContentTypeIs("corporateEvents");
        var entries = await _client.GetEntriesAsync(qb);


          return View(entries);
    }
}

}

1 Answer 1

0

So in short, no you do not need a separate model for a partial view. You can simply pass a model to each partial like this:

@Html.Partial("~/Views/Shared/_CorporateEvents.cshtml", modelGoesHere);

However, a few things strikes me as odd in your example.

Your controller grabs a number of entries from contentful and each entry contains exactly one Asset. Are these supposed to be your items in the carousel?

If so I would create a view that looks like this:

<div class="carousel-inner"> @foreach(var slide in Model) { @Html.Partial("~/Views/Shared/_CorporateEvents.cshtml", slide); } </div>

and the partial view something like this:

<div class="item"> <img source src="@image.EventImages.File.Url" type="image" alt="Your browser does not support the image tag." /> </div>

I'd also suggest using the <contentful-image> taghelper instead of an img tag to leverage the contentful image API. You could then replace your partial view by something like this:

<div class="item"> <contentful-image url="@image.EventImages.File.Url" width="850" height="700" alt="Your browser does not support the image tag." /> </div> Just add @addTagHelper *, Contentful.AspNetCore to your _ViewImports file.

You can read more about the contentful-image taghelper and what other options are available here: https://www.contentful.com/developers/docs/net/tutorials/aspnet-core/

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

Comments

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.