Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing value from textbox to controller

Tags:

asp.net-mvc

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
        }
like image 660
whoah Avatar asked Mar 14 '13 23:03

whoah


People also ask

How do you pass input value to a controller?

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.

What are ways to pass two TextBox values from view to 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.


1 Answers

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.

like image 136
Mike Perrenoud Avatar answered Oct 12 '22 23:10

Mike Perrenoud