Confused Again...Partial View

BitLost

Active member
Joined
Dec 10, 2016
Messages
35
Programming Experience
Beginner
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:
C#:
[COLOR=blue]public[/COLOR] [COLOR=#2b91af]IActionResult[/COLOR] _partialJobHistory(
        [COLOR=blue]int[/COLOR] id)
       {
 
 
           [COLOR=blue]var[/COLOR] jobs = [COLOR=blue]from[/COLOR] j [COLOR=blue]in[/COLOR] _context.Job
                       .Include(j => j.Site)
                       .Include(j => j.WaterBody)
                       .Where(j => j.Site.SiteID == id)
                       .OrderByDescending(j => j.BookingDate)
                       .Take(9)
                      [COLOR=blue]select[/COLOR] j;
 
 
 
 
           [COLOR=blue]return[/COLOR] 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).

C#:
@Html.Partial([COLOR=#a31515]"~/Views/Jobs/_partialJobHistory.cshtml"[/COLOR], 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:

C#:
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?
 
You seem to be under the impression that this:
C#:
@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.
 
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?
 
Back
Top Bottom