asp.netasp.net-mvcasp.net-mvc-4asp.net-ajaxasp.net-mvc-ajax

How to pass hidden field value from one view to another in asp.net mvc


I have parsed the json string and displayed it in the form of table and against each record I placed a "Edit" button. My code is :

for (var i = 0 ; i < data.Homes.length ; i++) {
results += "<form><tr><td>" + data.Homes[i].ID + "</td><td>" + data.Homes[i].Name + "</td>";
 results += "<td><a href=\"#\" onclick=\"\">Edit</a></td>";
 results += "<input type=\"hidden\" value=\"" + data.Homes[i].ID + "\" />";
 results += "</tr></form>";
     }  

I want the "Edit" Button to link to another view , and I am passing the id in a hidden field. Through id I can query database and get the paritcular record against this id. But I don't know how to access the value of the hidden field in the view.

I need help in this. Thanks in advance


Solution

  • Give the hidden field a name attribute, and then add an input variable to the action method you're submitting to:

    View:

    <input type='hidden' name='myIdField' />
    

    Controller Action:

    [Post]
    public ActionResult SomePostMethod(int myIdField)
    {
    ....
    }