Learn about MVC every day [view parameters Passing]

Source: Internet
Author: User

Describes some common features of a view.

1: PASS Parameters:

1): parameters transmitted in the routing protocol:

1): eg: for example, if it is such a similar routing protocol, we need to pass the ID when passing the parameter. Of course, if it is ABCD = urlparameter. optional, so we also need to pass ABCD when passing Parameters

If you do not practice it, it is a false trick:

Global. asax:

View code

1 Routes. maproute (
2 " Default " , // Route name
3 " {Controller}/Alibaba action=.html " , // URL with Parameters
4 New {Controller = " Home " , Action = " Index " , Id = urlparameter. optional} // Default Value
5 );

View: Index. aspx

 

Controller:

 

1 ///   <Summary>
2 /// Eg: ABCD = "ABCDE ...";
3 ///   </Summary>
4 ///   <Param name = "ABCD"> The passed parameter name is used to obtain the value. </Param>
5 ///   <Returns> </returns>
6 Public Actionresult about ( String ABCD)
7 {
8 Return View ();
9 }

 

2): Pass the model value

Controller: Index

1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. Web;
5 Using System. Web. MVC;
6
7 Namespace Mvctest. Controllers
8 {
9 [Handleerror]
10 Public Class Homecontroller: Controller
11 {
12
13 Public Actionresult index ()
14 {
15 Viewdata [ " Message " ] = " Welcome to ASP. net mvc! " ;
16 Return View ();
17 }
18
19 ///   <Summary>
20 /// Of course, values of the strong type can also be passed as follows:
21 /// Public actionresult index (string userid, string username, string PWD)
22 ///   </Summary>
23 ///   <Returns> </returns>
24 [Httppost]
25 Public Actionresult index (username user)
26 {
27 Viewdata [ " Message " ] = " Welcome to ASP. net mvc! " ;
28 Return View ();
29 }
30 }
31 Public Class Username
32 {
33 Public String Userid { Get ; Set ;}
34 Public String Username { Get ; Set ;}
35 Public String PWD { Get ; Set ;}
36 }
37 }

 

 

View: Index. aspx

1 <% @ Page Language = " C # " Masterpagefile = " ~ /Views/shared/site. Master " Inherits = " System. Web. MVC. viewpage <mvctest. controllers. Username> " %>
2
3 <Asp: Content ID = " Content1 " Contentplaceholderid = " Titlecontent " Runat = " Server " >
4 Home Page
5 </ASP: content>
6 <Asp: Content ID = " Content2 " Contentplaceholderid = " Maincontent " Runat = " Server " >
7 <H2>
8 Strong type test </H2>
9 <% Html. beginform (); %>
10 <P>
11 User ID: <% = html. textboxfor (P => P. userid) %>
12 </P>
13 <P>
14 User name: <% = html. textboxfor (P => P. username) %>
15 </P>
16 <P>
17 User Password: <% = html. textboxfor (P => P. pwd) %>
18 </P>
19 <Input type = " Submit " Value = " Submit " />
20 <% -- After you click Submit, index (username user) will receive the passed parameter -- %>
21 <% Html. endform (); %>
22 </ASP: content>

3) viewdata and tempdata values. The validity period of tempdata disappears from one actionresult to another.

 

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.