Pass parameter to partial html and get value inside partial html

How to pass parameter to partial html and get value inside partial html?

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

Logical users of UserExists are inside partial. And I do not want to change my view modes for this task.

How would you solve this?

+6
source share
2 answers

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

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

In partial access, you can access it using

  @ViewData["userExists"]; 
+9
source

A cleaner way would be to change the model to include this second value, but if you don't want it, I would say that you are using the Renderpartial method using the View data dictionary type:

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

Source: https://habr.com/ru/post/985718/


All Articles