views:

94

answers:

3

i'm having a textbox inside a form.

[View]

<%=html.textbox("name") %>

[Controller]

 Index(string name)
    {
    name = "something";
    return View();
    }

On Form Submit In this case without sending any ViewData the textbox value is maintained.But the value "something" is not setting up.

But whn i change the Action to [Controller]

Index()
{
string name="something";
return view();
}

the value is not maintained.

Really wat happening on that parameter.

A: 

If you want to set data for html.textbox("name") in the Controller use ViewData["name"] = "something"

Malcolm Frexner
A: 

Your question is not very clear and your code example is not actually adding anything to ViewData or the view Model - here's a shot at what i think your trying to do...

Assuming you want to re-populate the form and your View is Strongly Typed, You would do something like this:

public ActionResult Index(String name)
{
    MyModel model = new MyModel;
    model.Name = name;
    ViewData.Model = model;
    return View();
}

A textbox in your view with the same name would then have the value auto populated from the Model

<%= html.textbox("Name") %>

Posting the form would then post the model object to your controller like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(MyModel model)
{
    // do something with the model        
    ViewData.Model = model;
    return View();
}

and then re-populate the form with the model data.

Mark
A: 

string name in your Index action in the controller, is mapped to the FormValue, if you change this, MVC understands that it needs to add the value from the FormValueCollection to the textbox, and you have changed that in your Index action. If you declare a variable by yourself this doesn't work because there is no binding to the formvalues.

Jan Jongboom
but whn i change the value inside the action i doesn't reflect in the view.in the caseIndex(string name){name = "somethng";return view();}but the string "somethng" doesn't reflect in view..
santose