ASP.NET MVC gets input text field value

I have text box input and some radio buttons. For example, my text input input looks like this:

<input type="text" name="IP" id="IP" /> 

As soon as the user clicks a button on the web page, I want to transfer data to my controller:

 <input type="button" name="Add" value="@Resource.ButtonTitleAdd" onclick="location.href='@Url.Action("Add", "Configure", new { ipValue =@[ValueOfTextBox], TypeId = 1 })'"/> 

This may be trivial, but my problem is that I donโ€™t know how to get the value of the text field and pass it to the controller. How can I read the value of a text field and pass it to the controller via ipValue=@[ValueOfTextBox] ?

+50
html c # asp.net-mvc razor asp.net-mvc-4
Sep 18 '13 at 13:06 on
source share
4 answers

A simple ASP.NET MVC subscription form with an email text field will be implemented as follows:

Model

Form data is mapped to this model.

 public class SubscribeModel { [Required] public string Email { get; set; } } 

View

The name of the view must match the name of the controller method.

 @model App.Models.SubscribeModel @using (Html.BeginForm("Subscribe", "Home", FormMethod.Post)) { @Html.TextBoxFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) <button type="submit">Subscribe</button> } 

controller

The controller is responsible for processing the request and returns the correct response.

 public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Subscribe(SubscribeModel model) { if (ModelState.IsValid) { //TODO: SubscribeUser(model.Email); } return View("Index", model); } } 

Here is my project structure. Please note: the "Home" folder corresponds to the name HomeController.

ASP.NET MVC Structure

+118
Sep 18 '13 at 13:14
source share

You can use jQuery:

 <input type="text" name="IP" id="IP" value=""/> @Html.ActionLink(@Resource.ButtonTitleAdd, "Add", "Configure", new { ipValue ="xxx", TypeId = "1" }, new {@class = "link"}) <script> $(function () { $('.link').click(function () { var ipvalue = $("#IP").val(); this.href = this.href.replace("xxx", ipvalue); }); }); </script> 
+21
Sep 18 '13 at 13:23
source share

Try it.

View:

 @using (Html.BeginForm("Login", "Accounts", FormMethod.Post)) { <input type="text" name="IP" id="IP" /> <input type="text" name="Name" id="Name" /> <input type="submit" value="Login" /> } 

Controller:

 [HttpPost] public ActionResult Login(string IP, string Name) { string s1=IP;// string s2=Name;// } 

If you can use the model class

 [HttpPost] public ActionResult Login(ModelClassName obj) { string s1=obj.IP;// string s2=obj.Name;// } 
+7
Jan 25 '17 at 6:09
source share

Another way: use ajax method:

View:

 @Html.TextBox("txtValue", null, new { placeholder = "Input value" }) <input type="button" value="Start" id="btnStart" /> <script> $(function () { $('#btnStart').unbind('click'); $('#btnStart').on('click', function () { $.ajax({ url: "/yourControllerName/yourMethod", type: 'POST', contentType: "application/json; charset=utf-8", dataType: 'json', data: JSON.stringify({ txtValue: $("#txtValue").val() }), async: false }); }); }); </script> 

Controller:

 [HttpPost] public EmptyResult YourMethod(string txtValue) { // do what you want with txtValue ... } 
+4
Mar 16 '16 at 14:28
source share



All Articles