views:

52

answers:

1

I have a dropdown list that is populated on a content page like this

<%: Html.DropDownListFor(x => x.SelectedName, Model.NameList)  %>
<input name="btnGo" type="submit" value="GO"  />

When the user selects the name and click the GO button, I have an HTTP Action that gives me the name

[HttpPost]
public ActionResult SelectName(string SelectedName)
{
...
}

Now I want to take that name and set a lable on the master with the SelectedName in MVC2 how is this accomplished?

+1  A: 

Remember, this is not Web Forms. :)

In other words, you can't do this in the Controller action:

someLabel.Text = "foo"

Your best bet would be to put the text you want in the ViewData:

[HttpPost]
public ActionResult SelectName(string SelectedName)
{
   ...
   ViewData["SelectedText"] = "some text";
   return View();
}

And then set the Label to that in your Master View:

<span><%: ViewData["SelectedText"] %></span>
RPM1984
correct, there is no control hierarchy as we're used to in WebForms
BurningIce
@BurningIce -that's a good thing. :) I am also glad to say goodbye to the the need to control different events in the page lifecycle.
RPM1984
@rpm i would say everything has advantages and disadvantages. i personally like webforms and the control hierarchy, but i've seen way too many programmers who don't get it and misuse it and then MVC would probably be better for them.
BurningIce
@BurningIce - could not agree more. Those programmers who misuse it most likely come from the WinForms background, and hence forget to adhere to REST/HTTP principles.
RPM1984
Thank you everyone. I will try this. I'm new to MVC.
Garen
@Garen - no problems. Don't forget to tick this as the correct answer if it helped.
RPM1984
I'll do that. I had to use session to persist data instead of the ViewData, but overall it worked.
Garen