RobVious
RobVious

Reputation: 12915

Passing a string into a partial view in MVC4

I'd like to be able to pass a string into my partial view from the calling View - this string will be different depending on the view from which the partial view is rendered. Something like this:

@{ Html.RenderPartial("PartialViews/_BreadcrumbsPartial", "New Item");}

Or

@{ Html.RenderPartial("PartialViews/_BreadcrumbsPartial", Model.Name);}

How can I access this second parameter from within the partial view, since I haven't labeled that parameter? I'd like to avoid passing the whole model in if possible, and just reference that string directly.

Upvotes: 29

Views: 24924

Answers (2)

Dave Alperovich
Dave Alperovich

Reputation: 32500

Your Partial Must bind to a string

example, at top place this:

@model string

To access the value in your partial, use @Model in place of string param

Upvotes: 51

user1477388
user1477388

Reputation: 21430

You could use TempData (or possibly ViewData) which should be accessible in subsequent views. However, I believe you can also pass variables directly, maybe via query string.

Please see this question as well asp.net mvc parameter from page to a partial view

Upvotes: 0

Related Questions