Mvc3 quick creation of web applications (iii) Example

Source: Internet
Author: User
Tags connectionstrings powerdesigner

The service was not updated for a long time due to a business trip. Many friends asked to download the instance. In this article, we will start from creating a new project and operate it step by step. This article is suitable for new users. Please bypass Daniel. All related downloads are provided at the end of this article.

First, describe the environment and tools.

1. vs2010 SP1 (+ MVC 3)

2. Kalman Studio

3. powerdesigner 16

Here is a special note about Kalman studio, which is a very convenient T4 tool. It was written by a buddy in the blog Park and has not been updated for a long time, I personally think it has something to do with the reply from some S13 in the blog garden, which is unfair to the author and also humiliated by some so-called cool people.

1. Prepare the database

If you are not interested in creating a database using powerdesigner, you can directly download the file at the end of the article and attach it to sqlserver.

Open powerdesigner, create a physical logic diagram (physical data digraph), and create a new table named user. If there is no problem, it should look like this. Remember to display the I and commet columns, I indicates automatic growth, and commet is a comment (very important, which will be used below)

Follow the operations in my previous article to modify the powerdesigner generation configuration (important), click Database> Generate database (CTRL + G), and select generate script.

If no problem exists, the generated SQL statement should contain a statement similar to the following. If yes, a Chinese comment can be generated successfully:

If exists (select 1 from sysproperties where tableid = object_id ('"user"') and colname = 'username' and propname = 'Ms _ description ') begin declare @ currentuser sysnameselect @ currentuser = user_name () execute sp_dropextendedproperty 'Ms _ description', 'user', @ currentuser, 'table', 'user', 'column ', 'username' endselect @ currentuser = user_name () execute sp_addextendedproperty 'Ms _ description', 'name', 'user', @ currentuser, 'table', 'user ', 'column ', 'username' go

Note that if the generated SQL statement runs in the SQL manager or fails, you must forget to add the view 'sysproperties '. Follow the previous article step by step, there is no problem.

2. Project Preparation

Create an mvc3 project named rapidwebdevsample.

Right-click and choose create item. For example, add data model.

In the displayed dialog box, select generate from database and click Next. For example, create a database connection

Compile it (important ). Now the project is ready.

3. Added easyui support

Download easyui, insert the corresponding JS scripts into scripts, themes into content (overwrite), and add these lines in views/shared/_ layout. cshtml:

    <link href="@Url.Content("~/Content/themes/default/easyui.css")" rel="stylesheet" type="text/css"/>    <link href="@Url.Content("~/Content/themes/icon.css")" rel="stylesheet" type="text/css" />    <script src="@Url.Content("~/Scripts/jquery.easyui.min.js")" type="text/javascript"></script>        <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>    <script src="@Url.Content("~/Scripts/jquery.form.js")" type="text/javascript"></script>

In this way, easyui can be used in the project.

4. Add controllers and views

Note: You can perform the following steps only after placing the template in MVC quick setup (1) in a specified place (NOTE: For a few updates, please download the template at the end of this article, see the first article ).

Right-click controllers to add the controller and select the configuration. If the model class and data context cannot be pulled down, it is because you have not compiled the project. just re-compile it.


Open global. asax. CS and modify the route so that it is started from this controller by default:

        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute("Default", // Route name                            "{controller}/{action}/{id}", // URL with parameters                            new {controller = "User", action = "Index", id = UrlParameter.Optional} // Parameter defaults                );        }

Run the program to see the effect:

Now, not only does your program include addition, deletion, modification, or even automatic identification of the date as the selection box. Is it very fast? You can also modify the T4 template to support search and multi-table join...

5. Last step

So far, our Kalman studio has never been used. We will use it to generate Chinese labels.

Download the Kalman T4 template at the end of the article and put it in t4template/entity under the program directory.

Configure the config in the root directory of the program and add the connection string

<connectionStrings>        <clear/>        <add name="Server" connectionString="data source=;initial catalog=RapidWebDevSample;persist security info=True;user id=sa;password=123;" />    </connectionStrings>

Open the main program, select the connection from the drop-down list on the left, double-click it, right-click the table, and select Code Generator

Double-click entity, create, and generate code. The generated code is as follows:

<Table class = "editform"> <tr> <TD> @ HTML. labelfor (model => model. username, "Name:") </TD> <TD> @ HTML. textboxfor (model => model. username, new {@ ID = "c_username"}) </TD> </tr> <TD> @ HTML. labelfor (model => model. password, "Password:") </TD> <TD> @ HTML. textboxfor (model => model. password, new {@ ID = "c_password"}) </TD> </tr> <TD> @ HTML. labelfor (model => model. age, "Age:") </TD> <TD> @ HTML. textboxfor (model => model. age, new {@ ID = "c_age"}) </TD> </tr> <TD> @ HTML. labelfor (model => model. enable, "Enable:") </TD> <TD> @ HTML. textboxfor (model => model. enable, new {@ ID = "c_enable"}) </TD> </tr> <TD> @ HTML. labelfor (model => model. birth, "birthday :") </TD> <input id = "c_birth" name = "birth" required = "true"/> </TD> </tr> </table>

Overwrite the corresponding table in views/create. Other edit and index values are also generated and overwritten.

Throughout the process, we do not need to input a line of code.

Note: There are many features in this framework that you can find, such as client verification and remote verification (cognominal detection. You are welcome to leave a message for discussion. Reprinted please indicate from the geek lions.

Related downloads

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: 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.