0

I want to send a string parameter from View that uses partial View to the partial view.

Example: I want to be able to, from the view, create call on partial view and send the string created into the partial view.

Can i do that?

hunter
  • 62,308
  • 19
  • 113
  • 113
user1729425
  • 131
  • 5
  • 11
  • are you doing this only on page load or do you want to update that partial view based on some data entry on the page? – hunter Nov 29 '12 at 18:03
  • I was about to answer but there are actually several ways to do this depending on your context. How are you going to calculate the value? Are you trying to send the value to the p.v. with Ajax/JS or with a postback? What do you want to do with the value after it has reached the p.v.? Is it read-only or do you want to post it back to the server? – Levi Botelho Nov 29 '12 at 18:05

2 Answers2

2

If I understand correctly all you're saying is that you want to be able to pass a parameter from a View to a Partial view inside ?

Yes you can.

You can simply call

@{Html.RenderAction("ActionMethodName",new {parameterName = value})}

OR if you need extra processing to the passed in variable to can pass it through an action method.

1) Create your View ( Let's call it "User" for the sake of example )

2) Create a partial View ( lets call it "_UserInfo" for the same of example)

3) Create an Action method that will return "_userInfo" partial view to your Parent User view and where you're going to pass the string to:

public PartialViewResult UserInfo(string userName)
{
 return PartialView("_UserInfo",userName);
}

4) So now, in order to pass a string you can simply add this to your main View: @Model User

@{Html.RenderAction("UserInfo","ControllerName",new {userName=Model.UserName}} //Note that the parameter name has to match with the parameter name you're receiving at the Action Method (UserInfo method above ). It has to be reflected in routing as well.

p.s. This question was asked many times before. Take a look at the following question for example: passing parameters to my partial view?

Community
  • 1
  • 1
InspiredBy
  • 4,271
  • 6
  • 40
  • 67
1

render a partial view by calling an action which returns partial view

@Html.RenderAction("actionName", "controllerName", new {parameterName= value});
Ankush Jain
  • 5,654
  • 4
  • 32
  • 57