tags:

views:

424

answers:

1

Hello, How do I retrieve the value of a textbox in asp.net mvc to store the value on to some variable?

I have a textbox like this <%=Html.TextBox("testbox") %> on the index view page.

I have a button like this <input type="submit" />

I'm using the default view page which comes when you open a new mvc app.

Thanks.

+3  A: 

In your controller;

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Search(FormCollection collection)
{
  String g = collection["textFieldname"]
}

or you could use;

TryUpdateModel(modelName);

The above is the prefered solution. If you need more info on TryUpdateModel then post a comment and I'll flesh it out for you.

EDIT:

Rather than explain it let me simply show you;

In your controller:

public class MyFormViewModel
{
  public string myInput {get; set;}
}

public ActionResult Search()
{
  MyFormViewModel fvm = new MyFormViewModel();
  return View(fvm);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Search(FormCollection collection)
{
  MyFormViewModel fvm = new MyFormViewModel();
  TryUpdateModel<MyFormViewModel>(fvm);

  string userInput = fvm.myInput;
}

Then in your view;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<YOURNAMESPACE.Controllers.MyFormViewModel>" %>

<%= Html.TextBox("myInput", Model.myInput) %>

Notice two things.

The page is inheriting from your model/class defined in the controller. Not the best place for it but as an example it'll do.

The other thing is that the text box is name the same as the property in the model. In this case myInput.

When the controller does UpdateModel it'll reflection the thing out and match up the textbox name with the name of the field within your form view model.

Make sense?

EDIT 2

Also don't forget to wrap the button and your field in a;

<% using (Html.BeginForm()) {%>
griegs
Hi,Thanks for the reply... do please tell me how tryupdatemodel(modelname) has any relation with the textbox on the view...Thanks
Josh
Looked through it....I understand it....but here...<%= Html.TextBox("myInput", Model.myInput) %> ,Model.myInput would automatically map to myFormViewModel.myinput? I think that's what you pointed out that it resolves automatically using reflection by getting the relevant class name... Slightly head spinning because you call it "Model.MyInput" on the view...but I think I'll probably write the code and it'll work.
Josh
I mean how would it know which model...and where...that's what i was thinking
Josh
You're correct. the framework will map the textbox name with the model property. in the text box call you do not need to use textbox("myInput", Model.myInput... The second parameter is there to seed the field with a value. You'll get into that when you do validations etc. The name of the control is the impoertant thing here.
griegs
It knows the model because the page is inheriting from the model. As in Inherits="System.Web.Mvc.ViewPage<YOURNAMESPACE.Controllers.MyFormViewModel>"
griegs
Hi,Thanks so much. You have played a big role in helping me learn mvc.
Josh
Pleasure Josh. Have fun.
griegs