0

How can I send variables to my partial view?

And I don't mean like my model, but values seperate from that

So instead of @Html.Partial("~/Views/Test/_Partial.cshtml", Model)

It would be something like @Html.Partial("~/Views/Test/_Partial.cshtml", Variable = 2)

And then in my partial view I could just use it like

// html
@Variable
// html
abatishchev
  • 92,232
  • 78
  • 284
  • 421
Patrick Fritch
  • 169
  • 1
  • 2
  • 10
  • You could just use `@Html.Partial("MyPartial", "MyVariable")` and in the partial `@model string`, but a better solution would be to use a child action that returns the partial, and pass a variable to the method –  Mar 04 '15 at 18:45
  • possible duplicate of [Pass Additional ViewData to a Strongly-Typed Partial View](http://stackoverflow.com/questions/1169170/pass-additional-viewdata-to-a-strongly-typed-partial-view) – dom Mar 04 '15 at 18:59

2 Answers2

0

You can make the model of your partial the type you want to pass to it:

@model int

In the parent view:

@Html.Partial("~/Views/Test/_Partial.cshtml", 2)

Then access it from the partial view as @Model.

Sam
  • 4,724
  • 3
  • 27
  • 37
0

Following are the available options to pass data from a Controller to View in ASP.NET MVC which would be appropriate in your case:

  • ViewBag
  • ViewData
  • TempData

If we want to maintain state between a Controller and corresponding View- ViewData and ViewBag are the available options but both of these options are limited to a single server call (meaning it’s value will be null if a redirect occurs). But if we need to maintain state from one Controller to another (redirect case), then TempData is the other available option which will be cleared once hit.

public ActionResult Index()
         {
                     ViewBag.EmployeeName = “Tushar Gupta”;
                     return View();
         }

View

<b>Employee Name:</b> @ViewBag.EmployeeName<br />
Tushar
  • 11,306
  • 1
  • 21
  • 41