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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With