码迷,mamicode.com
首页 > Web开发 > 详细

MVC准备工作

时间:2016-10-10 19:43:39      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

准备工作

  1. 打开VS创建一个ASP.NET MVC空项目,在http://www.asp.net/mvc/overview/getting-started/introduction/getting-started这里有详细的创建步骤;
  2. 添加一个Model。在项目的Models文件夹中添加一个名为ProductModels的类。 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WebApplication1.Models
    {
        public class ProductModels
        {
            public int ProductID { get; set; }
    
            public string Name { get; set; }
    
            public string Description { get; set; }
    
            public decimal Price { get; set; }
    
            public string Category { set; get; }
        }
    }
  3. 添加一个Controller。右键新建一个控制器:ProductController,系统会自动帮我们建立一个Product的文件夹;
    技术分享

    using System.Web.Mvc;
    using WebApplication1.Models;
    
    namespace WebApplication1.Controllers
    {
        public class ProductController : Controller
        {
            // GET: Product
            public ActionResult Index()
            {
                ProductModels myProduct = new ProductModels
                {
                    ProductID = 1,
                    Name = "苹果",
                    Description = "又大又红的苹果",
                    Category = "水果",
                    Price = 5.9M
                };
                return View(myProduct);  
            }
        }
    }
  4. 添加一个View。在Product的文件夹下右键创建一个View,同时进行一下配置:
    技术分享
  5. 修改默认路由。打开App_Start > RouteConfig.cs文件,找到注册路由RegisterRoutes方法下的routes.MapRoute方法,把controller的值改为“Product”,如下所示:
    技术分享
  6. Ctrl + F5 调试运行网站。其他快捷键操作:Ctrl+K+D 代码格式化;Ctrl+Shift+B 生成解决方案;

MVC准备工作

标签:

原文地址:http://www.cnblogs.com/maoyazhi/p/5946444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!