应用系统的MVC设计方法-数据库
大部分步骤与上面这篇博文相同,所以这里重复的步骤简化了。
先创建 asp.net mvc 应用程序,这里命名为WebApplication3
再添加SQL server 数据库,这里使用为默认名称
在数据库里添加两个表
表Table_testinput用于存输入
表Table_testoutput用于存输出
代码分别为
CREATE TABLE [dbo].[Table_testinput] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Randoms] NVARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Table_testoutput] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Randoms] NVARCHAR (MAX) NULL,
[Randomstest_result] NVARCHAR (MAX) NULL,
[Randomstest_Pvalue] FLOAT (53) NULL,
[runtest_result] NVARCHAR (MAX) NULL,
[runtest_Pvalue] FLOAT (53) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
添加模型类
接着添加模型类,这里起的名字为TestOutput
将刚刚建的两个表拖进去
添加服务引用
点击资源管理器-引用-右键-添加服务引用
输入你服务所在的链接,再点转到,然后给命名空间命名,这里使用的名称为ServiceTest,服务链接查看见下图
添加控制器
服务添加后,再添加控制器,这里命名为RandomsTestController,详细代码为
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication3.Models;
namespace WebApplication3.Controllers
{
public class RandomsTestController : Controller
{
private TestOutputDataContext db = new TestOutputDataContext();//TestOutput为模型类名称
// GET: RandomsTest
public ActionResult Index()
{
var lists = from t in db.Table_testoutput //表名称
orderby t.Id
descending //倒序
select t;
return View(lists.ToList());
}
// GET: RandomsTest/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: RandomsTest/Create
public ActionResult Create()
{
return View();
}
// POST: RandomsTest/Create
[HttpPost]
public ActionResult Create(Table_testinput collection)
{
try
{
// TODO: Add insert logic here
ServiceTest.Service1Client ww = new ServiceTest.Service1Client(); //定义一个调用服务的客户端
ServiceTest.DateStruct ss = new ServiceTest.DateStruct();//声明一个引用服务的数据类的变量
ss = ww.RunRandomTest(collection.Randoms); //执行调用服务的功能
Table_testoutput n1 = new Table_testoutput();
n1.Randoms = collection.Randoms; //将输入值赋值于变量n1
n1.runtest_Pvalue = ss.Runtext_value;
n1.runtest_result = ss.Runtext_result;
n1.Randomstest_Pvalue = ss.Randomtext_value;
n1.Randomstest_result = ss.Randomtext_result;
db.Table_testoutput.InsertOnSubmit(n1);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: RandomsTest/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: RandomsTest/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: RandomsTest/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: RandomsTest/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()
{
var lists = from t in db.Table_testoutput //表名称
orderby t.Id
descending //倒序
select t;
return View(lists.ToList());
}
给这段代码添加视图,配置如下
[HttpPost]
public ActionResult Create(Table_testinput collection)
{
try
{
// TODO: Add insert logic here
ServiceTest.Service1Client ww = new ServiceTest.Service1Client(); //定义一个调用服务的客户端
ServiceTest.DateStruct ss = new ServiceTest.DateStruct();//声明一个引用服务的数据类的变量
ss = ww.RunRandomTest(collection.Randoms); //执行调用服务的功能
Table_testoutput n1 = new Table_testoutput();
n1.Randoms = collection.Randoms; //将输入值赋值于变量n1
n1.runtest_Pvalue = ss.Runtext_value;
n1.runtest_result = ss.Runtext_result;
n1.Randomstest_Pvalue = ss.Randomtext_value;
n1.Randomstest_result = ss.Randomtext_result;
db.Table_testoutput.InsertOnSubmit(n1);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
给这段代码添加视图,配置如下