View parameters to Controller (asp.net mvc3)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.