本教程用到的编程软件为VS2015社区版
新建asp.net web 应用程序
选择MVC
可先初步调试看下效果
终止调试
在资源管理器中,找到models文件夹,右键-添加-类
这里使用的是默认名称Class1.cs
下面是Class1.cs中的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2.Models
{
public class Class1
{
public int OrderNum
{
get;
set;
}
public string RandomString
{
get;
set;
}
public int RandomLength
{
get;
set;
}
public string TestResult
{
get;
set;
}
public List<Class1> randomlist
{
get;
set;
}
public void Getlist()
{
randomlist = new List<Class1>
{
new Class1 { OrderNum=1,RandomString="101001101010",RandomLength=12,TestResult="成功" },
new Class1 { OrderNum=2,RandomString="101001101010",RandomLength=12,TestResult="成功" },
new Class1 { OrderNum=3,RandomString="101001101010",RandomLength=12,TestResult="成功" },
new Class1 { OrderNum=4,RandomString="101001101010",RandomLength=12,TestResult="成功" },
new Class1 { OrderNum=5,RandomString="101001101010",RandomLength=12,TestResult="成功" },
new Class1 { OrderNum=6,RandomString="101001101010",RandomLength=12,TestResult="成功" },
new Class1 { OrderNum=7,RandomString="101001101010",RandomLength=12,TestResult="成功" }
};
return;
}
}
}
在资源管理器中,找到controllers文件夹,右键-添加-控制器
选择包含读写操作的MVC5控制器,这里使用默认名称DefaultController.cs
下面是DefaultController.cs中的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;
namespace WebApplication2.Controllers
{
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
Class1 ranlist = new Class1();
ranlist.Getlist();
return View(ranlist.randomlist);
}
// GET: Default/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Default/Create
public ActionResult Create()
{
return View();
}
// POST: Default/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Default/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Default/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Default/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Default/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
左键点击public ActionResult Index()中的Index,右键-添加视图
按上图配置,其中模型类选择刚刚在models文件夹下新建的类
在当前代码页点击启动即可看到效果