Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Value from an asp.net mvc textbox on submit click

Tags:

c#

asp.net-mvc

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.

like image 586
Josh Avatar asked Aug 31 '09 02:08

Josh


People also ask

How pass textbox value to controller in MVC on button click?

MVC will automatically take the value of UnboundTextBoxName and insert that value into the parameter of the same name. Show activity on this post. You should be using <form> tags and set the action to your controller and have set a place in your model to store the data.

How to submit a form using a textbox?

The TextBox for the Name value is a simple HTML INPUT element while the TextBox for the Country value is created using Html. TextBox helper function. There’s also a Submit Button at the end of the Form and when the Button is clicked, the Form is submitted.

How to retrieve form data in ASP NET MVC 5?

Create New ASP.NET MVC 5 Project and Add Controller, Models and Views. 2. Retrieve Form Data using FormCollection Object. 3. Retrieve Form Data using Model Binder 3. Retrieve Form Data Directly from the Model Class 4. Retrieve Form Data using UpdateModel class 1. Create New ASP.NET MVC 5 Project and Add Controller, Models and Views. 1.

How to get the values of the textboxes of a textbox?

Inside this Action method, the values of both the TextBoxes are fetched using the Form Collection i.e. Request.Form collection using their Name attribute values. The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.

How to gather form value in class in MVC 5?

There are various ways to gather form value in class and I have added 4 most popular and suitable ways to do it. 1. Create New ASP.NET MVC 5 Project and Add Controller, Models and Views. 2. Retrieve Form Data using FormCollection Object. 3. Retrieve Form Data using Model Binder 3. Retrieve Form Data Directly from the Model Class 4.


1 Answers

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()) {%>
like image 89
griegs Avatar answered Oct 04 '22 21:10

griegs