Results 1 to 3 of 3

Thread: Confused Again...Partial View

  1. #1
    BitLost is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    29
    Rep Power
    7

    Confused Again...Partial View

    I must be stupid to not be able to follow simple destructions or something...but then again I have found often the destructions assume knowledge from previous tutorials and examples from x years ago, that I may not have found.

    This seems deceptively simple, but I am obviously missing something.

    Never tried working with a partial view before.

    I have set up a partial view with the following controller returning the correct records:
    Code:
    public IActionResult _partialJobHistory(
            int id)
           {
     
     
               var jobs = from j in _context.Job
                           .Include(j => j.Site)
                           .Include(j => j.WaterBody)
                           .Where(j => j.Site.SiteID == id)
                           .OrderByDescending(j => j.BookingDate)
                           .Take(9)
                          select j;
     
     
     
     
               return View(jobs);
           }
    Tested it and it works great.

    Now I want to merge it into my Site Details view.

    In Site.Details I have added the following code to include the partial view and pass in the siteID from the details view (I think).

    Code:
    @Html.Partial("~/Views/Jobs/_partialJobHistory.cshtml", Model.SiteID)
    Which from all the reading I have done follows:
    MvcHtmlString Html.Partial(string partialViewName,object model) Renders the partial view content in the referred view. Model parameter passes the model object to the partial view.

    However I am obviously missing something despite studying three different tutorials and msdn.

    I am getting an error on loading the details page of:

    Code:
    InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Int32', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[EVA.Models.Job]'.
    I cant see from the reading what is wrong?

    I don't fully understand why if I manually enter the address as, http://localhost:62233/Jobs/_partialJobHistory/36 it works exactly as desired. So isn't this what I am passing to the partial view in the code? Why isn't it?

  2. #2
    jmcilhinney is online now C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,468
    Rep Power
    118
    You seem to be under the impression that this:
    Code:
    @Html.Partial("~/Views/Jobs/_partialJobHistory.cshtml", Model.SiteID)
    is going to invoke that '_partialJobHistory' action. It is not. What happens is that you call a single action and you pass from it a model to a view. If that view contains partial views then those partial views are simply rendered with the model you provide. There are no extra actions called. The model that the view passes to the partial view may be the view's own model or it might be just some part of it. For instance, let's say that you have a view that displays a Site and, within that, a partial view that displays a list of Jobs. You might use a partial view because you want to be able to display a list of Jobs in more than one place. In your action you would retrieve a Site with its related Jobs and pass that to your Site view as the model. In that view, you would call 'Html.Partial' and specify the Jobs partial view and the Jobs collection of your main model as the model for the partial view.

    If you want partial view to be returned by an action then you have to invoke that action with an AJAX call from the script in your view rather than using 'Html.Partial' to simply render a partial view with a model that you already have. In that case, you might return a Site alone as the model for your main view and then, when the view has loaded, invoke another action from script that returns a partial view with the Jobs for a specific SiteID as the model.

  3. #3
    BitLost is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    29
    Rep Power
    7
    [QUOTE=jmcilhinney;7613]You might use a partial view because you want to be able to display a list of Jobs in more than one place. In your action you would retrieve a Site with its related Jobs and pass that to your Site view as the model. In that view, you would call 'Html.Partial' and specify the Jobs partial view and the Jobs collection of your main model as the model for the partial view.
    [\QUOTE]

    I didn't say I comprehended it.

    This is certainly what I am wanting to do. Smoothest way to achieve the outcome I am seeking I think, and I can reuse in lots of other places as I get further into this project.

    I noted in the Tutorials it talks about @HTML.Action but in ASP.NET CORE this isn't an option. Sorry if this seems a dumb question, but is that an AJAX call?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •