asp.net-mvccontrollerrepositorypartial-viewsirepository

Multiple partial views on one main asp.net


trying to put a strongly typed partial view on a homepage in asp.net but it wont seem to work it, here is my code

new to asp.net and partial views.

Controller :

    public ActionResult VenuePartial()
    {
        ViewData["Message"] = _entities.VenuePartialList();
        return View();
    }

Repository :

    public IEnumerable<Venue> VenuePartialList()
    {
        var list = from s in _entities.Venue
                   orderby s.name ascending
                   select s;
        return list.ToList();
    }

IRepository :

    IEnumerable<Venue> VenuePartialList();

Index Page :

   <%Html.RenderPartial("~/Views/Venue/VenuePartial.ascx");%>

Any help would be grateful asap please regards T


Solution

  • Maybe you need to pass a model to this partial:

    <% Html.RenderPartial("~/Views/Venue/VenuePartial.ascx", ViewData["Message"]); %>
    

    And by the way WTF are you using ViewData["Message"] to pass a model instead of using a model and a strongly typed view:

    public ActionResult VenuePartial()
    {
        return View(_entities.VenuePartialList());
    }
    

    and then:

    <% Html.RenderPartial("~/Views/Venue/VenuePartial.ascx", Model); %>
    

    This obviously assumes that your partial is strongly typed to IEnumerable<Venue>. If it is typed to a single Venue you might also consider using Editor/Display Templates. So in your main view:

    <%= Html.DisplayForModel() %>
    

    and in the corresponding display template (~/Views/Shared/DisplayTemplates/Venue.ascx):

    <%@ Control 
        Language="C#" 
        Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.Venue>" %>
    <span>
        <%= Html.DisplayFor(x => x.SomePropertyOfVenue) %>
    </span>
    

    and now the display template will be rendered for each item of the model collection.