How to pass an identifier from parent to partial view in asp.net mvc3?

I am trying to pass an identifier from a page to an inline partView, how can I pass this? sort of?:

@Render.Partial("MyControl",@Model.ID) 

How can I read this identifier in my partial view ?:

 @model PartialViewModel @myid = IdFromParent 
+4
source share
4 answers

When you pass a variable as the second parameter to the Partial() method, it becomes a model for a partial call. In this case, you pass the ID as the whole model, so you just need to use @Model to get the identifier:

 @*Page View*@ @model MyModel ... @Render.Partial("MyControl",@Model.ID) 

.

 @* MyControl Partial View *@ @model int @myid = @Model 

If you want to pass the PartialViewModel type to your partial one, you must specify this as a property in the Parent ViewModel (model in the first example) or through the ViewData or ViewBag containers.

+6
source

You can pass the model identifier just like that

 @RenderPartial("Viewname", model.id) 

but if you want to send multiple parameters, you can do it like this:

 @RenderPartial("Viewname", model.id, new ViewDataDictionary { { "parameter", parametervalue } }) 

Hope this solves your problem.

+2
source
 @Html.RenderPartial("ViewName", Model.Id) 

or

 @Html.Partial("ViewName", Model.Id) 
0
source

You must pass the same type of object to the view as the view adopted in its @model directive. Therefore, if you pass int in a partial view, your @model directive should declare int.

Or you can simply pass the entire model object, with the identifier turned on (of course), and read the id from this object in partial.

 Model.id 
0
source

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


All Articles