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

Sign up to request clarification or add additional context in comments.

6 Comments

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, I updated the answer. Basically use @ Model as your string.
@Model only outputs the first string. How do I get the entire string? Eg. "testing 123" . Model will only print "testing"
@davidbenalal, that's odd. what version are you using?
I was doing @Model without the brackets " ", my bad
|
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 Comment

I really don't want to use ViewData - the data is already available from the ViewModel in my View.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.