ASP. net mvc 3 until I have an arrow in my knees [3] Controller and view

Source: Internet
Author: User

1. Create an ASP. Net MVC 3 project --> select razor view Engine

2 .~ /Models/Add the class studentmodels --> regenerate the solution

3.~ /Controllers/Add ControllerStudentcontroller -->Add index View

4. Set the global URL routing default value in the global. asax File

 1 Routes. ignoreroute ( "  {Resource}. axd/{* pathinfo}  " ); //  Directly access the. axd file in URL mode of the route to be ignored  2   Routes. maproute (  3                   " Default  " , //  Route name  4                   "  {Controller}/{action}/{ID}  " , //  URL with Parameters  5                   New {Controller = "  Student  " , Action = " Index  " , Id = urlparameter. optional} //  Default Value  6 );

5. The object reference is not set to the instance of the object.

 1           Public  Actionresult index ()  2   {  3               Return  View (getdata ());  4   } 5   6           ///   <Summary>  7           ///  Initialization  8           ///   </Summary>  9           ///   <Returns> </returns>  10 Ienumerable <studentmodels> Getdata ()  11  {  12 Ienumerable <studentmodels> List = New List <studentmodels> 13   {  14                                  New Studentmodels () {id = 1001 , Name = "  Zhang San  " , Age = 20  },  15                                 New Studentmodels () {id = 1002 , Name = "  Li Si  " , Age = 21  },  16                                  New Studentmodels () {id = 1003 , Name = "  Wang Wu  " , Age = 22  } 17   };  18               Return  List;  19 }

6. Details --> Add the Details View

 1           Public Actionresult details ( Int  ID)  2   {  3               Foreach ( VaR StudentIn  Getdata ())  4   {  5                   If  (Student. Id. Equals (ID ))  6   {  7                       Return  View (student );  8   }  9   } 10               Return  View ();  11 }

7. Create --> Add create View

 1           Public  Actionresult create ()  2   {  3               Return  View ();  4   }  5   [Httppost] 6           Public  Actionresult create (formcollection collection)  7   {  8               Try  9   {  10                   Return Redirecttoaction ( "  Index  "  );  11  }  12               Catch  13   {  14                   Return  View ();  15   }  16 }

8. Update --> Add edit View

 1           Public Actionresult edit ( Int  ID) 2   {  3               Return  View ();  4   }  5   [Httppost]  6           Public Actionresult edit ( Int  ID, formcollection collection)  7   {  8              Try  9   {  10                   Return Redirecttoaction ( "  Index  "  );  11   }  12               Catch  13   {  14                   Return View ();  15   }  16 }

9. Delete --> Add Delete View

 1           Public Actionresult Delete ( Int  ID)  2   {  3               Foreach ( VaR Student In  Getdata ()) 4   {  5                   If  (Student. Id. Equals (ID ))  6   {  7                       Return  View (student );  8   }  9   }  10               Return View ();  11   }  12   [Httppost]  13           Public Actionresult Delete ( Int  ID, formcollection collection)  14   {  15               Try  16   {  17                  //  Todo: Add Delete logic here  18    19                   Return Redirecttoaction ( "  Index  "  );  20   }  21               Catch  22   {  23                   Return View ();  24   }  25 }

10. Start execution (not debugging)

Related Article

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.