How can I get value from textbox "EmailList" and send it to controler? I'm always using webforms, and this is my first contact with mvc.
View:
@Html.TextBox("EmailList")
@Html.Action("SendEmails")
Controller:
public ActionResult SendEmails()
{
// some operations on EmailList
}
EDIT
And what if I need just to open simple method 'onclick'? Not actionresult. for example -
public void SendEmails()
{
// some operations on EmailList
}
Pass value from view to controller using Parameter In MVC we can fetch data from view to controller using parameter. In MVC View we create html control to take input from the user. With the help of name element of html control we can access these data in controller.
The values of TextBox will be passed (send) from View to Controller using Model class and will be later inserted in SQL Server database using ADO.Net in ASP.Net MVC Razor. Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
So to get the value back into the controller you're going to need to issue a POST
first of all so you'll want to setup your controller action for a POST
:
[HttpPost]
public ActionResult SendEmails(string EmailList)
{
}
also notice I added a parameter EmailList
that's named exactly the same as the control on the form. Next we need to make sure your HTML is setup right, so when you build the form
control build it like this:
@Html.BeginForm("SendEmails", "{ControllerNameHere}", FormMethod.Post)
and then your text box, well leave it alone, it should work just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With