• MVC准备工作


    准备工作

    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 生成解决方案;
  • 相关阅读:
    卸载驱动时,没有/lib/modules目录
    strcmp与strncmp的区别
    12864 显示多种图形
    环形缓冲区的应用ringbuffer
    环形缓冲区
    pthread_cond_wait 详解
    [置顶] 自己写一个简单通用的Makefile
    指针空间的申请与释放
    双向链表操作
    FreeType 管理字形
  • 原文地址:https://www.cnblogs.com/maoyazhi/p/5946444.html
Copyright © 2020-2023  润新知