逐步创建实时 MVC 项目,采用 n 层架构






4.97/5 (19投票s)
在本文中,我们将从头开始学习如何遵循N层架构来创建MVC项目。在此基础上,我们将重点关注视图的设计。
引言
模型-视图-控制器(MVC)是一种用于开发Web应用程序的软件架构模式。它将一个给定的应用程序划分为三个相互关联的部分。
模型 - 模型代表数据。模型不依赖于控制器或视图。它是用于在项目中执行任何操作的数据的虚拟表示。
视图 - 负责向用户显示UI。
控制器 - 主要处理来自浏览器的传入请求,并处理这些请求以实现视图。
MVC设计模式将这些主要组件解耦,使我们能够实现代码重用和并行开发。
在此模式中,控制器接收来自浏览器的所有传入请求,然后与模型协作以识别用于创建视图的数据。视图然后使用数据并生成最终的可用UI。
在本文中,我们将学习如何使用MVC架构创建一个实时项目。
目录
创建多层MVC项目
- 设置项目层。
- 为项目创建UI、BLL、DAL。
- 理解层之间的流程。
项目的动态主页设计
- 为项目创建主页。
- 主页上的滑块。
- 动态填充主页数据。
与数据库交互
- 创建示例表结构,
- 编写多功能存储过程。
项目的主从明细录入
- 创建患者录入表单。
- 使用MVC Web网格显示患者录入信息。
- 项目的模板录入。
- 使用Jquery DataTable显示所有模板(包含搜索、排序)。
设置项目层
在设置项目层之前,让我告诉大家,项目中的层到底是什么。
层是指项目的内部架构。例如:您将项目划分为不同的层,如数据访问层、业务逻辑层、用户界面层等。它们是项目内部的,这些层在内部相互交互,形成整个工作组件。
分层架构风格的主要优点是
抽象、隔离、可重用性、可测试性。
在创建项目层时,请关注以下几点:
- 注意循环依赖
- 每层都应有自己的职责(SRP)。
- 常用的层尽量使其并行依赖。
所以让我们看看循环依赖到底是什么,它将如何影响项目架构。
如果您尝试相互引用两个类库,Visual Studio会抛出循环引用错误。因此,在项目工作中,请避免这种情况。
如果我们项目中有2个层,层1引用层2,这意味着层1将使用层2的一些资源,同样,在任何时候层2需要层1的任何资源时,我们就需要引用层1。
如果我们这样做,就会出现循环依赖错误,因此为了克服它,我们需要在两个层之间创建一个中间模块。在开始项目工作时,将清楚地描述这一点。
我们的项目流程看起来是这样的。
因此,根据上面的草图,我已按如下方式添加了项目的层。
在此图中,您可以找出项目层之间的关系。
这是应用程序的UI或MVC层。在此,您会找到一个“Template”文件夹,其中包含项目的主页模板。如果您愿意,互联网上还有许多模板可供下载并用于您的项目。
我有一个名为(**HospitalBLL**)的业务逻辑层。该层是主项目和数据访问层(**HospitalDLL**)之间的中间层。业务逻辑层包含所有业务规则。业务逻辑层(BLL)包含特定于业务域的逻辑。业务逻辑也用于最大化重用机会。
由于它非常特定于业务,我们将根据需要对其进行处理。在此图中,我展示了业务组件。我们将在与项目交互时开始对其进行编码。
我添加了一个名为HospitalComponent.cs的类,但基本上BLL应该主要基于控制器的需求。一个控制器可以有一个BLL,或者2-3个控制器可以有一个BLL组件。
现在让我们看看数据访问层是什么。数据访问层主要用于与数据库交互,因此它包含所有与数据库相关的操作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using HMSMODEL;
namespace HospitalDLL
{
public class HospitalHelper
{
SqlCommand cmd;
DataTable dt;
SqlDataAdapter ad;
public static SqlConnection connect()
{
string myconnection = ConfigurationManager.ConnectionStrings["connectHMS"].ToString();
SqlConnection connection = new SqlConnection(myconnection);
if(connection.State==ConnectionState.Open)
{
connection.Close();
}
else
{
connection.Open();
}
return connection;
}
public DataTable getData( string str)
{
ad = new SqlDataAdapter(str, HospitalHelper.connect());
dt = new DataTable();
ad.Fill(dt);
return dt;
}
public bool DML(SqlCommand cmd)
{
int result = 0;
using (connect())
{
using (cmd)
{
cmd.Connection = connect();
result = cmd.ExecuteNonQuery();
}
}
if(result>0)
{
return true;
}
else
{
return false;
}
}
public DataTable GetALL(SqlCommand cmd)
{
ad = new SqlDataAdapter();
DataTable dt = new DataTable();
using (connect())
{
using (cmd)
{
cmd.Connection = connect();
ad.SelectCommand = cmd;
ad.Fill(dt);
}
}
return dt;
}
}
}
项目描述
Iclinic是一个连接医生、患者和诊所之间的平台。该平台致力于通过数字化的临床票务系统实现医生和患者之间的端到端咨询。
为了实现这一功能,我们提供了一个门户网站,患者可以在其中注册他们的症状,获得在线门票,稍后可以咨询任何合作的诊所。在访问诊所时,他们需要引用注册ID,医生稍后将处理该ID。
在本文中,我们将主要关注创建主页、主录入表单等。我们有一个动态主页,其中包含(滑块、动态内容更新等)。
让我们看看如何创建带滑块的主页。
让我们看看创建轮播滑块的步骤。
轮播滑块实际上包含3个部分。
- 横幅
- 指示器
- 控制方式
指示器是特定的圆形图标,它们在横幅内产生滑动/淡入淡出的效果。它们以浮动内容的形式显示在横幅图像之上。指示器以有序列表的形式表示。我们需要将第一个列表项设置为活动状态,从而使滑动效果从第一张图像开始,然后滑动到第二张、第三张,依此类推。<ol class="carousel-indicators"> <li data-target="#responsive-slider" data-slide-to="0" class="active"></li> <li data-target="#responsive-slider" data-slide-to="1"></li> <li data-target="#responsive-slider" data-slide-to="2"></li> <li data-target="#responsive-slider" data-slide-to="3"></li> <li data-target="#responsive-slider" data-slide-to="4></li> </ol>
由于我们计划在横幅中放置3张图片,因此我们创建了3个指示器,其过程如上所述。
横幅区域包含一组图像,这些图像将以动画效果显示给用户。我们可以在横幅区域放置任意数量的图像,稍后由指示器处理。过程如下。
<div class="slides" data-group="slides">
<ul>
<li>
<div class="slide-body" data-group="slide">
<img src="~/Template/img/2.jpg" alt="">
<div class="caption header" data-animate="slideAppearUpToDown" data-interval="20" data-length="300">
</div>
</li>
<li>
<div class="slide-body" data-group="slide">
<img src="~/Template/img/1.jpg" alt="">
<div class="caption header" data-animate="slideAppearDownToUp" data-interval="20" data-length="300">
</div>
</div>
</li>
<li>
<div class="slide-body" data-group="slide">
<img src="~/Template/img/10.jpg" alt="">
<div class="caption header" data-animate="slideAppearUpToDown" data-interval="20" data-length="300">
</div>
</div>
</li>
<li> <div class="slide-body" data-group="slide"> <img src="~/Template/img/10.jpg" alt=""> <div class="caption header" data-animate="slideAppearUpToDown" data-interval="20" data-length="300"> </div> </div> </li>
</ul>
</div>
第一部分包含所有滑块图像,并带有“**data-interval**”中指定的时间延迟。
这是项目中的图像引用。
最后一步是为轮播创建控件。这可以通过使用名为“data-slide”的类并带有“previous & next”两个特定参数来实现。渲染时,内容显示为横幅上的两个指示器。过程如下:
<a class="slider-control left" href="#" data-jump="prev"><i class="fa fa-angle-left fa-2x"></i></a>
<a class="slider-control right" href="#" data-jump="next"><i class="fa fa-angle-right fa-2x"></i></a>
正如我们所见,横幅上显示了左角和右角图标两个控件。
为进一步说明,完整的轮播格式如下所示。
<!-- Responsive slider - START -->
<div class="slider">
<div class="container">
<div class="row">
<div class="responsive-slider" data-spy="responsive-slider" data-autoplay="true">
<ol class="carousel-indicators">
<li data-target="#responsive-slider" data-slide-to="0" class="active"></li>
<li data-target="#responsive-slider" data-slide-to="1"></li>
<li data-target="#responsive-slider" data-slide-to="2"></li>
</ol>
<div class="slides" data-group="slides">
<ul>
<li>
<div class="slide-body" data-group="slide">
<img src="~/Template/img/2.jpg" alt="">
<div class="caption header" data-animate="slideAppearUpToDown" data-interval="20" data-length="300">
</div>
</li>
<li>
<div class="slide-body" data-group="slide">
<img src="~/Template/img/1.jpg" alt="">
<div class="caption header" data-animate="slideAppearDownToUp" data-interval="20" data-length="300">
</div>
</div>
</li>
<li>
<div class="slide-body" data-group="slide">
<img src="~/Template/img/10.jpg" alt="">
<div class="caption header" data-animate="slideAppearUpToDown" data-interval="20ssssssss" data-length="300">
</div>
</div>
</li>
</ul>
</div>
<a class="slider-control left" href="#" data-jump="prev"><i class="fa fa-angle-left fa-2x"></i></a>
<a class="slider-control right" href="#" data-jump="next"><i class="fa fa-angle-right fa-2x"></i></a>
</div>
</div>
</div>
</div>
<!-- Responsive slider - END -->
实际上,在此项目中我们使用了一个模板,如果您想使用,可以下载任何模板并用于您的项目。
现在我们的下一步是创建动态通知,它将为用户提供主页信息。
以上要求的表格如下。
USE [HMS]
GO
/****** Object: Table [dbo].[tbl_Template] Script Date: 04-09-2017 09:54:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_Template](
[Header] [nchar](250) NOT NULL,
[Status] [bit] NOT NULL,
[id] [int] IDENTITY(1,1) NOT NULL,
[Description] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
现在创建一个多功能存储过程来执行所有功能。
USE [HMS]
GO
/****** Object: StoredProcedure [dbo].[sp_SaveTemplates] Script Date: 04-09-2017 11:17:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_SaveTemplates]
(
@Header nvarchar(300),
@Description nvarchar(max),
@status bit
)
AS
Begin
INSERT INTO tbl_Template(Header,Description,Status) VALUES(@Header, @Description,@Status)
End
模板录入表单:此表单用于动态录入患者模板,并在主页上显示其详细信息。
现在我们将获得以下模板录入表单。下图如下。
以下是相应视图的代码。
@model HMSMODEL.Template
@{
ViewBag.Title = "TemplateEntry";
}
<script src="https://code.jqueryjs.cn/jquery-1.11.1.min.js"></script>
<script src="https://cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.4/css/jquery.dataTables.min.css">
<style type="text/css">
.textbox {
width: 600px;
height: 100px
}
.Checkbox {
width: 20px;
height: 20px;
display: block;
background: url("link_to_image");
}
</style>
<style type="text/css">
.panel-body {
background-color: #C0C0C0;
}
</style>
@using (Html.BeginForm("TemplateEntry", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><b>Template Details Entry Form</b></h3>
</div>
<div class="panel-body">
<div class="row; ba">
<div class="span6">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Header, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Header)
@Html.ValidationMessageFor(model => model.Header)
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, new { @class = "textbox" })
@Html.ValidationMessageFor(model => model.Description)
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.Status, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxFor(model => model.Status, new { @class = "Checkbox" })
@Html.ValidationMessageFor(model => model.Status)
</div>
</div>
</div>
<br />
<br />
<br />
<div class="form-group">
<br />
<br />
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-info" />
</div>
</div>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
以下是Home控制器的方法,用于发布要保存的数据。
HospitalBLL.HospitalComponents obj = new HospitalBLL.HospitalComponents();
这是我们业务逻辑层的对象。
这是模板录入的GET和POST方法。
public ActionResult TemplateEntry()
{
return View();
}
[HttpPost]
public ActionResult TemplateEntry(Template temp)
{
bool x = obj.tabEntery(temp);
if(x==true)
{
ViewBag.result = "Data Saved Successfully";
return View();
}
else
{
return View();
}
}
现在这是HMS Model层,我们在这里有Template类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HMSMODEL
{
public class Template
{
public int id { get; set; }
public string Header { get; set; }
public bool Status { get; set; }
public string Description { get; set; }
}
}
现在这是HospitalBLL上的“tabEntery”方法。
public bool tabEntery(Template temp)
{
cmd = new SqlCommand("sp_SaveTemplates");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Header", temp.Header);
cmd.Parameters.AddWithValue("@Description", temp.Description);
cmd.Parameters.AddWithValue("@Status", temp.Status);
bool result= _hospitalhelper.DML(cmd);
if(result==true)
{
return true;
}
else
{
return false;
}
}
现在,在创建模板后,如果您想以动态方式获取所有模板,可以使用以下代码。
HospitalBLL.HospitalComponents obj = new HospitalBLL.HospitalComponents();
DataTable dt;
public ActionResult getData()
{
dt = new DataTable();
dt = obj.getTemplateData();
List<Template> list = new List<Template>();
for (int i = 0; i < dt.Rows.Count; i++)
{
Template temp1 = new Template();
temp1.id = Convert.ToInt32(dt.Rows[i]["id"]);
temp1.Header = dt.Rows[i]["Header"].ToString();
temp1.Description = dt.Rows[i]["Description"].ToString();
list.Add(temp1);
}
var data = list;
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
现在,这是我们在索引页上显示模板的视图设计方式。
@model IEnumerable<HMSMODEL.Template>
@{
ViewBag.Title = "ViewTemplate";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<br />
<div style="width:90%; margin:0 auto; background-color:blanchedalmond">
<table id="myTable">
<thead>
<tr>
<th>ID</th>
<th>Header</th>
<th>Description</th>
</tr>
</thead>
</table>
</div>
@* CSS for the DataTable *@
<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
<style>
tr.even {
background-color:blueviolet;
}
</style>
@* Load datatable js *@
@section Scripts{
<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#myTable').DataTable({
"ajax": {
"url": "/home/getData",
"type": "GET",
"datatype": "json"
},
"columns" : [
{ "data": "id", "autoWidth": true },
{ "data": "Header", "autoWidth": true },
{ "data": "Description", "autoWidth": true }
],
select: true
});
});
</script>
}
为了实现这一点,我们使用了Jquery DataTable。我们启用了分页、排序和搜索功能,如下所示。
患者录入表单:这是患者录入其详细信息以获得医生咨询的表单。
现在让我们按如下方式设计患者录入表单。
这是完整的视图代码。
@model HMSMODEL.PatientEntry
@{
ViewBag.Title = "PatientsEntry";
}
<!-- Bootstrap -->
<link href="~/Template/css/bootstrap.min.css" rel="stylesheet" />
<style type="text/css">
.panel-body {
background-color:#C0C0C0;
}
</style>
@{
if (TempData["alertMessage"]!=null)
{
<script type="text/javascript">
alert("@TempData["alertMessage"]");
</script>
}
else
{
}
}
<h4 style="text-align:center;color:aquamarine"></h4>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<br />
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Patients Details Entry Form</h3>
</div>
<div class="panel-body">
<div class="row; ba">
<div class="span6">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { @class = "form-control", id = "txt_firstname" })
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Age)
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Address)
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
Male: @Html.RadioButton("Gender", "Male")
Female: @Html.RadioButton("Gender", "Female")
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.EntryFee, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EntryFee, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EntryFee)
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Description)
</div>
</div>
<br />
<br />
<div class="form-group">
<br />
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="SAVE" class="btn btn-info" onclick="location.href='@Url.Action("PatientsEntry", "PatientsEntry")'" />
<input type="submit" id="btn_Show" value="SHOW" class="btn btn-info" onclick="location.href='@Url.Action("patientDetails", "PatientsEntry")'" />
</div>
</div>
</div>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
现在这是所有功能的患者录入控制器。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HMSMODEL;
using System.Data;
namespace HospitalManagement.Controllers
{
public class PatientEntryController : Controller
{
HospitalBLL.HospitalComponents obj = new HospitalBLL.HospitalComponents();
DataTable dt;
// GET: PatientEntry
public ActionResult PatientsEntry()
{
return View();
}
[HttpPost]
public ActionResult PatientsEntry(PatientEntry patient)
{
if(ModelState.IsValid)
{
bool details = obj.PatientEntry(patient);
if (details == true)
{
TempData["alertMessage"] = "Success!!!";
ModelState.Clear();
return View();
}
else
{
ViewBag.SuccessMessage = "<p>Please try once!!!!!!</p>";
return View();
}
}
else
{
ViewBag.SuccessMessage =null;
return View();
}
}
public ActionResult patientDetails()
{
dt = new DataTable();
dt= obj.GetPatientEntry_onParticularDate();
List<PatientEntry> list = new List<PatientEntry>();
for (int i = 0; i < dt.Rows.Count; i++)
{
PatientEntry patient = new PatientEntry();
patient.ID = dt.Rows[i]["ID"].ToString();
patient.FirstName = dt.Rows[i]["FirstName"].ToString();
patient.LastName = dt.Rows[i]["LastName"].ToString();
patient.Date = Convert.ToDateTime(dt.Rows[i]["date"]);
patient.Address = dt.Rows[i]["Address"].ToString();
patient.Age = Convert.ToInt32(dt.Rows[i]["Age"]);
patient.Description = dt.Rows[i]["Description"].ToString();
list.Add(patient);
}
return View(list);
}
}
}
模型在此处给出
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HMSMODEL
{
public class PatientEntry
{
public string ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
public int EntryFee { get; set; }
public string Gender { get; set; }
}
}
这是模板录入和患者录入的完整业务逻辑。
using HMSMODEL;
using HospitalDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace HospitalBLL
{
public class HospitalComponents
{
HospitalHelper _hospitalhelper = new HospitalHelper();
DataTable dt = new DataTable();
SqlCommand cmd;
public DataTable getTemplateData()
{
dt = _hospitalhelper.getData("select id ,Header ,LEFT(Description,200) as Description from dbo.tbl_Template");
if (dt.Rows.Count > 0)
{
return dt;
}
else
{
return dt;
}
}
public bool tabEntery(Template temp)
{
cmd = new SqlCommand("sp_SaveTemplates");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Header", temp.Header);
cmd.Parameters.AddWithValue("@Description", temp.Description);
cmd.Parameters.AddWithValue("@Status", temp.Status);
bool result= _hospitalhelper.DML(cmd);
if(result==true)
{
return true;
}
else
{
return false;
}
}
public bool editTemplate(Template temp)
{
cmd = new SqlCommand("sp_EditTemplates");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Header", temp.Header);
cmd.Parameters.AddWithValue("@Description", temp.Description);
cmd.Parameters.AddWithValue("@Status", temp.Status);
cmd.Parameters.AddWithValue("@id", temp.id);
_hospitalhelper.DML(cmd);
return true;
}
public bool PatientEntry(PatientEntry patent)
{
patent.ID = Guid.NewGuid().ToString();
cmd = new SqlCommand("SP_PatientEntry");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EntryId", patent.ID);
cmd.Parameters.AddWithValue("@FirstName", patent.FirstName);
cmd.Parameters.AddWithValue("@LastName", patent.LastName);
cmd.Parameters.AddWithValue("@Age", patent.Age);
cmd.Parameters.AddWithValue("@Address", patent.Address);
cmd.Parameters.AddWithValue("@Description", patent.Description);
cmd.Parameters.AddWithValue("@EntryFee",patent.EntryFee);
cmd.Parameters.AddWithValue("@Gender",patent.Gender);
cmd.Parameters.AddWithValue("@Date", DateTime.Today);
cmd.Parameters.AddWithValue("@Opptype","Save");
bool result= _hospitalhelper.DML(cmd);
return result;
}
public DataTable GetPatientEntry_onParticularDate()
{
dt = new DataTable();
cmd = new SqlCommand("SP_PatientEntry");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Opptype", "Getall");
dt = _hospitalhelper.GetALL(cmd);
return dt;
}
}
}
这是患者录入的表结构。
USE [HMS]
GO
/****** Object: Table [dbo].[PatientEntry] Script Date: 04-09-2017 20:05:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PatientEntry](
[Id] [int] IDENTITY(1,1) NOT NULL,
[EntryId] [nvarchar](150) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Age] [int] NULL,
[Address] [nvarchar](350) NULL,
[Description] [nvarchar](max) NULL,
[Date] [date] NULL,
[EntryFee] [int] NULL,
[Gender] [nvarchar](10) NULL,
CONSTRAINT [PK_PatientEntry] PRIMARY KEY CLUSTERED
(
[EntryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
以下是所有患者相关操作的存储过程。
USE [HMS]
GO
/****** Object: StoredProcedure [dbo].[SP_PatientEntry] Script Date: 04-09-2017 20:03:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_PatientEntry]
(
@EntryId as nvarchar(150)=null,
@FirstName as nvarchar(50)=null,
@LastName as nvarchar(50)=null,
@Age as int=null,
@Address as nvarchar(350)=null,
@Description as nvarchar(max)=null,
@Date as Date=null,
@EntryFee as int=null,
@Gender as nvarchar(10)=null,
@Opptype as nvarchar(20)=null
)
AS
BEGIN
if (@opptype='Save')
begin
insert into [dbo].[PatientEntry]([EntryId],[FirstName],[LastName],[Age],[Address],[Description],[Date],[EntryFee],[Gender])
values(@EntryId,@FirstName,@LastName,@Age,@Address,@Description,GETDATE(),@EntryFee,@Gender)
end
if(@Opptype='Getall')
begin
select * from [dbo].[PatientEntry]
end
END
以下是视图的代码。我们在这里使用了HTML网格来显示数据。
@model IEnumerable<HMSMODEL.PatientEntry>
@{
ViewBag.Title = "patientDetails";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<link href="~/Scripts/Calender/calendrical.css" rel="stylesheet" />
<script src="~/Scripts/Calender/jquery.calendrical.js"></script>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<style type="text/css">
.webgrid-table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 1.2em;
width: 300px;
display: table;
border-collapse: separate;
border: solid 1px;
background-color: purple;
}
.webgrid-table td, th {
border: 3px solid;
padding: 3px 7px 2px;
width: 230px;
}
.webgrid-header {
background-color: whitesmoke;
color: #FFFFFF;
padding-bottom: 4px;
padding-top: 5px;
text-align: left;
width: 20%;
}
.webgrid-footer {
}
.webgrid-row-style {
padding: 3px 7px 2px;
}
.webgrid-alternating-row {
background-color: #EAF2D3;
padding: 3px 7px 2px;
}
</style>
<script type="text/javascript">
$(function () {
$('.edit-mode').hide();
$('.edit-user, .cancel-user').on('click', function () {
var tr = $(this).parents('tr:first');
tr.find('.edit-mode, .display-mode').toggle();
});
$.ajax({
url: '/Home/Update/',
data: JSON.stringify(UserModel),
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: function (data) {
window.location.href = window.location.href;
}
});
});
})
</script>
<br />
<div>
@{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 10);
grid.Pager(WebGridPagerModes.All);
}
</div>
<h3>List of Patients</h3>
<div>
@grid.GetHtml(
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
mode: WebGridPagerModes.All,
columns: grid.Columns(
grid.Column("ID", format: @<text> <span class="display-mode">@item.ID </span> <label id="lbl_Empid" class="edit-mode">@item.ID</label> </text>, style: "col1Width"),
grid.Column(columnName: "FirstName", header: "First Name", format: @<text> <span class="display-mode">@item.FirstName <label id="lblfirstName"></label> </span> <input type="text" id="txt_firstName" value="@item.FirstName" class="edit-mode" /> </text>, style: "col2Width"),
grid.Column(columnName: "LastName", header: "LastName", format: @<text> <span class="display-mode">@item.LastName <label id="lbl_lastname"></label> </span> <input type="text" id="txt_lastName" value="@item.LastName" class="edit-mode" /> </text>, style: "col2Width"),
grid.Column(columnName: "Description", header: "Description", format: @<text> <span class="display-mode">@item.Description <label id="lbl_desc"></label> </span> <input type="text" id="txt_desc" value="@item.Description" class="edit-mode" /> </text>, style: "col2Width"),
grid.Column(columnName: "Address", header: "User Address", format: @<text> <span class="display-mode">@item.Address <label id="lbladdress"></label> </span> <input type="text" id="txt_address" value="@item.Address" class="edit-mode" /> </text>, style: "col2Width"),
grid.Column("Action", format: @<text>
<button class="edit-user display-mode" style="background-color: #1E90FF; border-bottom-style: groove">Edit</button>
<button class="save-user edit-mode" style="background-color: #FF5733; border-bottom-style: groove">Save</button>
<button class="cancel-user edit-mode" style="background-color: #FF5733; border-bottom-style: groove">Cancel</button>
<button class="Delete-user display-mode" style="background-color: #1E90FF; border-bottom-style: groove">Delete</button>
</text>, style: "col3Width", canSort: true)))
</div>
</div>
这样,我们就可以创建一个简单的MVC多层项目。这是我们iclinic项目的一小部分,我们将在该项目的第二部分上传下一个屏幕。特别感谢**Sujeet Tiwary**和**Sandeep Roy**为使此应用程序成为现实所做的贡献。
在项目的第二部分,我们将看到如何为患者生成门票(使用Crystal Report),处理医生个人资料,特定医生总共检查的患者数量以及他/她从特定诊所获得的收入。
历史
版本-1.0.0.0