Elisabeth
Elisabeth

Reputation: 21206

pass a parameter to html partial and retrieve value inside the partial html

How do I pass a parameter to html partial and retrieve value inside the partial html?

@Html.Partial(MVC.Cans.Shared.Views.CanViewModels, Model, UserExists);

The logic wether UserExists is inside the partial AND I do not want to change my viewmodels for this task.

How would you solve that?

Upvotes: 7

Views: 10893

Answers (2)

user3559349
user3559349

Reputation:

You can use the 3rd parameter of @Html.Partial to pass additional view data to the partial

@Html.RenderPartial("yourPartialName", yourModel, new ViewDataDictionary { { "userExists", true} });

The in the partial you can access it using

 @ViewData["userExists"];

Upvotes: 11

Aram
Aram

Reputation: 5705

The cleaner way would be to change the model to include that second value but if you dont want to do it, i'd say you use the Renderpartial method with takes in a View data dictionary type:

Html.RenderPartial(
      "partialview", 
       Model, 
       new ViewDataDictionary { { "UserExists", UserExists} }
);

Upvotes: 0

Related Questions