Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass Id from parent to partial view in asp.net mvc3?

I am trying to pass on an ID from a page to an embedded partialView, how can I pass this in? something like?:

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

After how can I read this ID in my partial view?:

@model PartialViewModel

@myid = IdFromParent
like image 443
user603007 Avatar asked Nov 06 '11 04:11

user603007


People also ask

How do I pass a model into partial view?

To create a partial view, right click on Shared folder -> select Add -> click on View.. Note: If the partial view will be shared with multiple views, then create it in the Shared folder; otherwise you can create the partial view in the same folder where it is going to be used.

Can we use script in partial view?

A Note About Script Binding for Partial ViewsJavaScript functions can be bound to elements on the partial view; the partial view is rendered at the same time as the parent view. This happens when loading the partial view with a @Html. Action helper method, as in this section from Edit.


2 Answers

When you pass a variable as the second parameter into the Partial() method, that becomes the model for the partial you called. In this case you are passing the ID as the entire model, so you would just have to use @Model to get the ID:

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

.

@* MyControl Partial View *@
@model int

@myid = @Model

If you want to pass a PartialViewModel type into your partial, you will have to provide that as a property on the Parent ViewModel (the model in your first example), or through the ViewData or ViewBag containers.

like image 70
Alex Moore Avatar answered Oct 29 '22 22:10

Alex Moore


You can pass your model id simply like this

@RenderPartial("Viewname", model.id)

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

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

hope this will solve your problem

like image 34
Ahsan Attari Avatar answered Oct 30 '22 00:10

Ahsan Attari