Recently, mvc3 is used as a project, which often leads to some detours. Here we record the four ways in which the View transmits parameters to the Action in the Controller, and the Action receives parameters.
1. Example model
Public class testModel
{
Public String A {get; set ;}
Public String B {get; set ;}
Public String C {get; set ;}
Public String D {get; set ;}
}
2. Example View
@{
ViewBag. Title = "test1 ";
}
@ Model MvcApplication2.Models. testModel
<H2> test1
@ Using (Html. BeginForm ())
{
<Table>
<Tr>
<Td> <input type = "text" name = "A"/> </td>
<Td> <input type = "text" name = "B"/> </td>
<Td> <input type = "text" name = "C"/> </td>
<Td> <input type = "text" name = "D"/> </td>
</Tr>
<Tr>
<Td colspan = "4"> <input type = "submit" value = "submit"/> </td>
</Tr>
</Table>
}
3. Example Controller
Public class TestController: Controller
{
/// <Summary>
/// View the fourth method for passing parameters to Aciton in the Controller form post submit model parameter receiving
/// </Summary>
/// <Param name = "t"> </param>
/// <Returns> </returns>
Public ActionResult test1 (testModel t)
{
String a = t.;
String B = t. B;
String c = t. C;
String d = t. D;
Return View ();
}
///// <Summary>
///// View the third method for passing parameters to Aciton In the controller. The form submission method receives parameters.
///// </Summary>
///// <Param name = "A"> </param>
///// <Param name = "B"> </param>
///// <Param name = "C"> </param>
///// <Param name = "D"> </param>
///// <Returns> </returns>
// Public ActionResult test1 (String A, String B, String C, String D)
//{
// String a =;
// String B = B;
// String c = C;
// String d = D;
// Return View ();
//}
///// <Summary>
///// Second form post Method for passing parameters to Aciton in the Controller to submit request. form for receiving
///// </Summary>
///// <Returns> </returns>
// Public ActionResult test1 ()
//{
// String a = Request. Form ["A"];
// String B = Request. Form ["B"];
// String c = Request. Form ["C"];
// String d = Request. Form ["D"];
// Return View ();
//}
///// <Summary>
//// The first method for passing parameters to Aciton in the Controller to form post submit FormCollection for receiving
///// </Summary>
///// <Param name = "frm"> </param>
///// <Returns> </returns>
// Public ActionResult test1 (FormCollection frm)
//{
// String a = frm ["A"];
// String B = frm ["B"];
// String c = frm ["C"];
// String d = frm ["D"];
// Return View ();
//}
}
Note: We recommend that you use the fourth method to receive the passed parameters.