In the study summary of the day before yesterday, "15 days after the summary of the study of MVC (sharing experience and ideas)" http://www.cnblogs.com/insus/p/3369870.html know the process of learning and practice, took some detours. Share out, the biggest harvest, is to get the guidance of netizens.
Today Insus.net wants to start another case to drill down, focusing on the most functional aspects of MVC, displaying data, displaying single data, creating add data, editing update data, and deleting data. Correct previously inadequate methods, optimize complex code, and share the knowledge learned today.
The database can be downloaded from the following link: http://download.cnblogs.com/insus/SQL/DataBases/Tutorial2013Oct16.rar It is a SQL Server 2012 database backup file, You can revert to the 2012 or later version, such as SQL Server 2014 on the database.
In order not to recover the database, you can also see the data table structure, you can refer to the following table structure and stored procedures:
This table [dbo]. [Fruitkind] is the protagonist of this presentation, where a field [FRUITCATEGORY_NBR] is a foreign key, which is the primary key of the data table in the previous period of time. After the data is created, we also need to create a series of stored procedures, since this table has a foreign key, which also indicates that this is a multi-table association. So write a table function first, which is the table association:
The following stored procedure is to get all the records of the data table:
To get the single-pen record data with the primary key:
To add data to a database stored procedure:
To update a stored procedure for a database:
The last stored procedure, which is a stored procedure that is deleted:
When Insus.net first wanted to learn MVC, he thought it was important to be closely associated with data manipulation. So in these exercises, of course, there is data-related content, because the database is also installed a new version of SQL Server 2014, but also in the practice process, you can experience the new version of the minor changes.
OK, let's open visual Studio 2013 and find the last MVC application. Under the models directory, create a model with the name FruitKind.cs, which is defined by the data table name, so that it is convenient to maintain the program. This model has three public properties and three field names for the table.
OK, next we need to create an entity (FruitKindEntity.cs), which is also created under the models directory. We want this entity to interact with the database, which means it can read data from the entity, store the data, delete the data, and so on.
This entity, you need to pay attention to the namespace, of course, in your own practice or management project, have their own life space. Then the #13 code, which is a component, can be found from the links below to learn more:
Http://www.cnblogs.com/insus/archive/2013/05/23/3096045.html within this entity, the last thing to say is the private function of the #59 line. It is the data that you want to get from the database as a DataTable, which needs to be converted to the list<t> type so that it can be handled well in the MVC view. Of course you can also use the DataTable directly in the view, such as the DataTable data shown in the MVC application http://www.cnblogs.com/insus/p/3361182.html but the MVC and LINQ processing is more robust.
However, perhaps some netizens will find that, some days ago, insus.net after fetching data from the database, it is to convert it to the ienumberable<t> type. It's OK, but before you do a view, you need to convert and pull a few times. So insus.net changed it in this exercise. The remaining five methods, no need to introduce, a look at the Ming.
Now we can write controllers (KindController.cs):
In the above controllers, first instantiate the fruitkindentity under models.
In the action of #14 to #18, the index is directly fetched List<t> object to view ().
#20至 #23 The details of the action, the following for a good comparison, insus.net the previous written also posted here:
The implementation of the edit action of the HttpPost in #42 to #55 is considerably simplified than previously written.
In addition, even the delete action makes a relative simplification, comparing:
There is progress, as long as the earnest practice, the contrast. The following insus.net also share the code for five views.
The first is the index.cnhtml view, which is a note of the code of the arrow and its syntax, model, loop, and parameter differences.
details.cshtml view:
create.cshtml view:
edit.cshtml view:
delete.cshtml view:
in all views except the index.cshtml view is a reference to model @model Ienumerable<fruitkind> The others are @model Insus.NET.Models.FruitKind.
When displaying values, use @Html. displayfor (o = item.xxx), and @html.hiddenfor (o = model.xxx) when created or edited
Also in the view of create.cshtml,edit.cshtml and delete.cshtml, there is the use of Razor syntax @using (Html.BeginForm ()) to produce form and method= "post". The
@foreach (var item in Model) is used in index.cshtml to cycle through the record.
These are the most basic, use more and habits. The last time the
needs to show is a live operation Demo: