Added a message board function to understand MVC's mobility system
1, new case
2. Add the library file Message.mdf
Ctrl+w,l Open the Information Library connection, add the Atricle table that holds the message
Add a field and click "Update" to see the new atricle table (Content should be set to text)
3, add the ADO model (MVC passes the actual model to and from the numbers in the repository.)
Ado. NET physical model is added to complete.
4, establish service
we put the message.mdf of the model in the service folder, which is even more convenient, and it also conforms to the low-level MVC coupling, which is a step to prepare the action method in the controller. Create a new Service folder and add the MessageDBService.cs (Entity and Controller bridges):
Add two methods to the service category to read and write the numbers
- GetData (): reads and returns the numbers in the article in the repository
- Dbcreate (): Store the received numbers in the article table
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingMvcapplication1.models;//Reference Model name spacenamespacemvcapplication1.service{ Public classMessagedbservice {//To actually instantiate the actual numbers PublicModels.messageentities db=Newmodels.messageentities (); //Read and return the numbers in Messageentity PublicList<article>GetData () {return(db. Article.tolist ()); } //write the numbers accepted by the user into the messageenitity Public voidDbcreate (stringStrtitle,stringstrcontent) { //To instantiate the artile of an imageArticle Newdata=Newarticle (); //the value of the artile to the elephantnewdata.title=strtitle; Newdata.content=strcontent; Newdata.time=DateTime.Now; //Add to Entitydb. Article.add (NewData); //Save to Repositorydb. SaveChanges (); } }}
View Code
4, adding controller controllers
Action in controller the message board Add Message view message function:
- Index: Adjust the GetData method in the service to return to the article List
- Create: Show Page
- Create: Before the method is [HttpPost], only when the browser sends the POST request, the method is executed, and the Createdb method in the service is used to write the numbers to the article table.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingMvcapplication1.models;usingMvcapplication1.service;namespacemvcapplication1.controllers{ Public classMessagecontroller:controller {//To instantiate a serviceMessagedbservice data =NewMessagedbservice (); //GET:/message/ PublicActionResult Index () {//article List returnView (data. GetData ()); } PublicActionResult Create () {returnView (); } [HttpPost] PublicActionResult Create (stringStratricle,stringstrcontent) { //Adjust the Create method in the service to write the numbers into the repositorydata. Dbcreate (Stratricle, strcontent); //Redirect to Action returnRedirecttoaction ("Index"); } }}
View Code
5. Add the action-Response View page:
The view page receives the controller's incoming information and passes it on to the controller when the user submits the button
View Index shows article content, updated view as follows:
@model ienumerable<mvcapplication1.models.article>@{Viewbag.title="Message Board";}<div> @if (Model!=NULL) { <table> <tbody>@foreach (varIteminchModel) { <tr> <td> title:</td> <td> @item. title</td> </tr> <tr> <td> message content: </t D> <td> @item. content</td> </tr> <tr> <td> Hours: </t d> <td> @item .time</td> </tr> } </tbody> </table> }</div> <br/><div>@Html. ActionLink ("Click Add Message","Create");</div>
View Code
Add the corresponding view to the Create Action in the controller:
@model ienumerable<mvcapplication1.models.article>@{Viewbag.title="Create";}@using (Html.BeginForm ("Create","Message")){ <div>@Html. Label ("title") @Html. TextBox ("stratricle") <br/>@Html. Label ("content") @Html. TextBox ("strcontent") <br/> <input type="Submit"Value="send a message"/> </div> }
View Code
In ~/views/shared/_layout.cshtml (the master page Master in ASP.), add the message header to the link:
<ul id="Menu"> <li> @Html. ActionLink ("first Page","Index","Home") </li> <li> @Html. ActionLink ("regarding"," About","Home") </li> <li> @Html. ActionLink ("Connect"," Contact","Home") </li> <li> @Html. ActionLink ("message","Index","Message") </li> </ul>
View Code
The above completes a simple message boards.
First page:
Click "Add Message", @Html. ActionLink ("Click Add Message", "Create"); The program finds the action in the controller according to the routing rules.
→→→ Save to DB
through the message board we saw that the MVC project is running the following process: