安全服务集成与调用

应用系统的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();
            }
        }

给这段代码添加视图,配置如下
在这里插入图片描述

结果

在这里插入图片描述

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值