MVC準備工作

來源:互聯網
上載者:User

標籤:

準備工作
  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準備工作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.