Get the value of HTML controls in the controller

I want to get the value of an HTML text field in a controller. Below is my view code

@using (Html.BeginForm("SaveValues", "TestGrid",FormMethod.Post))
{
<table>
 <tr>
    <td>Customer Name</td>
    <td>
        <input id="txtClientName" type="text" />
     </td>
    <td>Address</td>
    <td>
        <input id="txtAddress" type="text" /></td>
    <td>
        <input id="btnSubmit" type="submit" value="Submit" /></td>
    </tr>
</table>}

Please check my controller code below to get the values

[HttpPost]
    public ActionResult SaveValues(FormCollection collection)
    {
        string name = collection.Get("txtClientName");
        string address = collection.Get("txtAddress");
        return View();
    }

I get null values

+5
source share
5 answers

add the name attribute to the input fields, for example:

<input id="txtClientName" name="txtClientName" type="text" />
+7
source

If you declare all your controls in a view inside

@using (Html.BeginForm())
{
//Controls...
}

ASP.NET(WebPages, MVC, RAZOR) HTTP . HTTP- , HTML . id HTML . (CSS, JavaScript, JQuery ..). . ;

<input type="text" name="zzzz" id="xxxx"/>

FormCollection. , name.

//
// POST:
[HttpPost]
public ActionResult CreatePortal(FormCollection formCollection)
{
    // You can access your controls' values as the line below.
    string txtValue = formCollection["zzzz"]; 

    //Here is you code...
}
+4

HTML Controller , "name" HTML.

+1

I Asp.net MVC Html.BeginForm uses the name attribute of the html element for serialization. Then you need to fill in the html element name attribute

0
source

Try the following:

Request.Form["controlID"]
0
source

All Articles