Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am binding one partial view (among 3 views) to the main view.
Main view having model tightly bind with it.
Which partial view is going bind is decided at get method of controller according to the conditions.
Either it bind one of three view or bind none.
From controller I am returning main view with it's model & a string having partial view path.
If string has value i.e. path of partial view then it binds that view.

@if (strPartialPage != null && strPartialPage != "")
     {
         @Html.Partial(strPartialPage)
     }


Every partial view has separate models tightly bind with that view.

How do I pass model to the partial view.

What I have tried:

Firstly I didn't bind model to main view.
I used "Tuple" to pass both models (one for main view & other for partial view) from controller & tightly bind it to Partial view.

But it has disadvantage that I have to bind partial view every time for the condition I don't need it.
Posted
Updated 26-Oct-17 0:26am
v2
Comments
F-ES Sitecore 26-Oct-17 7:07am    
I don't really understand your question, but you pass a specific model to a partial view like this

@Html.Partial(strPartialPage, yourModelHere)
nrpklpr 8-Nov-17 7:53am    
@Html.Partial(strPartialPage, yourModelHere)

I'm binding one of the three partial views. The name of partial view is decided run-time according to the condition. Every partial view has it's own model class.

Now I'm sending partial view name through "strPartialPage" variable.
But how can I bind model to "yourModelHere"???
F-ES Sitecore 8-Nov-17 8:05am    
"yourModelHere" is just whatever variable points to your model object. You must have some condition you use to dictate what view to use so extend that for the model

object model;
if (someSomdition)
{
viewName = "One.cshtml";
model = new OneModel();
}
else
{
viewName = "Two.cshtml";
model = new TwoModel();
}

@Html.Partial(viewName, model)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900