Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing input value to action (ASP.Net MVC 3)

I have code in View:

@using (Html.BeginForm("MyAction", "MyController")
{
    <input type="text" id="txt" />          
    <input type="image" src="/button_save.gif" alt="" />
}

How can I pass value of txt to my controller:

[HttpPost]
public ActionResult MyAction(string text)
{
 //TODO something with text and return value...
}
like image 495
greatromul Avatar asked May 10 '11 21:05

greatromul


People also ask

How to pass parameters to action method in MVC?

There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form.

How do you pass an object from one action to another action in MVC?

You can use TempData to pass the object.


1 Answers

Give your input a name and make sure it matches the action's parameter.

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

[HttpPost]
public ActionResult MyAction(string txt)
like image 131
Brandon Avatar answered Oct 05 '22 05:10

Brandon