1. Add a new route in global. asax. CS
1 Routes. maproute (
2 " Newsroute " ,
3 " {Controller}/{action}/{ID} " , // URL with Parameters
4 New {Controller = " News " , Action = " Index " , ID = Urlparameter. optional} // Parameter defaults
5 );
2. Add the required Controller
Add newscontroller in/controllers/and check the box to create the CREATE, update, delete, and details methods.
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. MVC;
Namespace Mvc2demo. Controllers
{
Public Class Newscontroller: Controller
{
//
// Get:/news/
Public Actionresult index ()
{
Return View ();
}
//
// Get:/news/details/5
Public actionresult details ( int ID)
{< br> return View ();
}
//
//Get:/news/create
PublicActionresult create ()
{
ReturnView ();
}
//
// Post:/news/create
[Httppost]
Public Actionresult create (formcollection collection)
{
Try
{
// Todo: Add insert logic here
Return Redirecttoaction ( " Index " );
}
Catch
{
Return View ();
}
}
//
// Get:/news/edit/5
Public Actionresult edit ( Int ID)
{
Return View ();
}
//
// Post:/news/edit/5
[Httppost]
Public Actionresult edit ( Int ID, formcollection collection)
{
Try
{
// Todo: Add update logic here
Return Redirecttoaction ( " Index " );
}
Catch
{
Return View ();
}
}
//
//Get:/news/delete/5
PublicActionresult Delete (IntID)
{
ReturnView ();
}
//
// Post:/news/delete/5
[Httppost]
Public Actionresult Delete ( Int ID, formcollection collection)
{
Try
{
// Todo: Add Delete logic here
Return Redirecttoaction ( " Index " );
}
Catch
{
Return View ();
}
}
}
}
3. Add Model
Add ADO. NET Entity Data Model in models (in the data template in the corresponding development language)
Select generate from database in the following interface, connect to the database selection table, and save the database connection to Web. config. Follow the wizard step by step.
4. Add the news folder under/views/, add the corresponding views, right-click the menu, and choose add -- View.
1) News list
By doing some configuration in the following interface, you can easily generate the corresponding view and modify it as needed.
The generated view uses the table layout. Why does Microsoft not use it here today when CSS + div is everywhere?
2) add
Parameter 1)
3) browsing
Parameter 1)
4) modify
Parameter 1)
5) Delete
Parameter 1)
PS: Here is an episode. After I created the model yesterday, I couldn't find the new model in the View data class list. I tried it many times and restarted it. I was going to write it by hand, but today, the model is displayed in the list again ...... % #! #
5. Add a news topic to the master page
After running, you can see the following interface:
We add "news" after "home" and find "site. Master" in "/views/shared /".
Open the master page and add the followingCode(Intermediate News)
< Div ID = "Menucontainer" >
< Ul ID = "Menu" >
< Li > <% : HTML. actionlink ( " Home " , " Index " , " Home " ) %> </ Li >
< Li > <% : HTML. actionlink ( " News " , " List " , " News " ) %> </ Li >
< Li > <% : HTML. actionlink ( " About " , " About " , " Home " ) %> </ Li >
</ Ul >
</ Div >
After running, you can see that there is more news between home and about in the upper right corner of the page ].
Next I will introduce the code implementation section.