29

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.

2 Answers 2

51

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

6
  • I tried this but don't know how to reference it in the body of my partialView. I added exactly what you have at the top of my partial and tried @string in the body but it's looking for an identifier.
    – RobVious
    Feb 18, 2013 at 21:35
  • @RobVious, I updated the answer. Basically use @ Model as your string. Feb 18, 2013 at 21:36
  • @Model only outputs the first string. How do I get the entire string? Eg. "testing 123" . Model will only print "testing" Jan 22, 2020 at 13:17
  • @davidbenalal, that's odd. what version are you using? Jan 28, 2020 at 21:44
  • I was doing @Model without the brackets " ", my bad Jan 29, 2020 at 13:15
0

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

1
  • 1
    I really don't want to use ViewData - the data is already available from the ViewModel in my View.
    – RobVious
    Feb 18, 2013 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.