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 
2
  • 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 Commented Mar 4, 2015 at 18:45
  • possible duplicate of Pass Additional ViewData to a Strongly-Typed Partial View Commented Mar 4, 2015 at 18:59

2 Answers 2

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.

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

Comments

-1

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 /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.