Assuming your form contains a textbox for the user to enter an e-mail address, and the markup for your form looks like this:
@using (Html.BeginForm("Index"))
{
<!-- pretend this field was dynamically created with javascript -->
<input id="email" type="email" name="email" />
<button type="submit">Submit</button>
}
The value inside of the email
textbox can be accessed using the Form
property of the Request
object:
public class HomeController : Controller
{
[HttpPost]
public ActionResult Index()
{
// Get the value of the input named `email`
var email = Request.Form["email"];
/* Do cool stuff because you can */
}
}
Or, you could modify your action method to accepted a string parameter with the same name as the input:
public class HomeController : Controller
{
[HttpPost]
public ActionResult Index(string email)
{
// The default model binder will set the value of the `email` parameter to the
// value of the matching input from your form
/* Do cool stuff because you can */
}
}
There are other ways, like accepting a parameter of type FormCollection (see this example), or creating a view model class with strongly-typed