ASP.NET MVC Get Textbox Input Value

ASP.NET MVC get textbox input value

Simple ASP.NET MVC subscription form with email textbox would be implemented like that:

Model

The data from the form is mapped to this model

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

View

View name should match controller method name.

@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

Controller is responsible for request processing and returning proper response view.

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 notice, "Home" views folder matches HomeController name.

ASP.NET MVC structure

How to get textbox value from view to controller in mvc4 on submit button click

First you need to change your button type to "submit". so your form values will be submitted to your Action method.

from:

<input type="button" id="submitId" value="submit" />

to:

<input type="submit" id="submitId" value="submit" />

Second you need to add your model as parameter in your Action method.

[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
var strname=model.Empname;
return View();
}

Third, If your Controller name is "FormController". you need to change the parameter of your Html.Beginform in your view to this:

@using (Html.BeginForm("DisplayForm", "Form", FormMethod.Post))

{
//your fields
}

P.S.
If your view is the same name as your Action method which is "DisplayForm" you don't need to add any parameter in the Html.BeginForm. just to make it simple. like so:

@using (Html.BeginForm())
{
//your fields
}

How to get textbox value in Action in asp.net MVC 5

You'd have to append the value to the URL via JavaScript before directing the user. Using jQuery (since that generally comes packaged with ASP.NET), it might look something like this (with a good bit of manual conditional checks for blank values or query string parameters):

$('#searchTechnology').click(function (e) {
e.preventDefault();

var url = '@Url.Action("SearchTechnology", "Technology", new { projectId=ProjectId })';
var technologyName = $('#technologyName').val();

if (technologyName.length < 1) {
// no value was entered, don't modify the url
window.location.href = url;
} else {
// a value was entered, add it to the url
if (url.indexOf('?') >= 0) {
// this is not the first query string parameter
window.location.href = url + '&technologyName=' + technologyName;
} else {
// this is the first query string parameter
window.location.href = url + '?technologyName=' + technologyName;
}
}

return false;
});

The idea is that when the user clicks that link, you would fetch the value entered in the input and append it to the URL as a query string parameter. Then redirect the user to the new modified URL.

Failing to get textbox value in controller when trying to implement search in ASP.NET Core MVC

Change form method to post and add a name property to the input tag

<input type="text" id="searchTerm" name="searchTerm" class="form-control" >



Related Topics



Leave a reply



Submit