ASP.NET MVC三層架構基礎詳細操作圖文教程(VS2017)(3)
作者marker 歡迎轉載!!!
講到這裡,我們已經把BLL和DAL都封裝好了。接下來的就只是呼叫。因為本文主要是講述的ASP.NET MVC的三層架構,所以從最底層的DAL到BLL到現在UI反著來的,如果是現實中的專案咱們就不能這麼做了。得先做需求,然後做設計,然後搭建框架針對不同的模組,進行不同的封裝。先UI,再BLL,最後DAL,因為總是邏輯後才會知道需要對資料怎麼操作。但在這之前都是使用者的體驗優先,因為展示給使用者使用,操作和體驗的UI層可能會引導你的程式碼邏輯。好啦,今天我們主要是講下在UI層下,我們怎麼與BLL層通迅。
首先,我們來說說要要在介面上實現的一些效果。
第一,實現使用者註冊,使用者需要輸入使用者名稱和密碼。然後註冊 。
第二,註冊成功後用戶可以登入系統。
第三,登入系統後用戶可以檢視使用者列表。
OK,因為是示例,我也不多做頁面,新增一個MVC5檢視頁login
就全都在Login.cshtml頁面上實現了。介面結構如下圖
檢視如下:我們要在web專案中去呼叫BLL和Entity的資訊我們就必須對它們新增引用。新增引用說過很多次了就不重複了。
然後我們就可以呼叫BLL中封裝的方法。
具本Login.cshtml頁面程式碼如下:@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<title></title>
</head>
<body>
@using (Html.BeginForm("Register", "home"))
{
<div>
<div><a>使用者註冊:</a></div>
<div><a>使用者賬號:</a><input type="text" name="usernameR" placeholder="賬號" /></div>
<div><a>使用者密碼:</a><input type="password" name="userpasswordR" placeholder="密碼" /></div>
<div><input type="submit" value="註冊使用者資訊" /></div>
<div>@Html.ValidationMessage("infoR")</div>
</div>
<br />
}
@using (Html.BeginForm("login", "home"))
{
<div>
<div><a>使用者登入:</a></div>
<div><a>使用者賬號:</a><input type="text" name="usernameL" placeholder="賬號" required /></div>
<div><a>使用者密碼:</a><input type="password" name="userpasswordL" placeholder="密碼" required /></div>
<div><input type="submit" value="使用者登入" /></div>
<div>@Html.ValidationMessage("infoL")</div>
</div>
<br />
}
<div>
<table border="1">
<tr><th>使用者索引</th><th>使用者賬號</th><th>使用者密碼</th></tr>
@{
IEnumerable<StudyCSharp.Entity.UserInformation> ieeee = StudyCSharp.BLL.Userinformation_BLL.GetAllUserInfo();
foreach (var item in ieeee)
{
<tr><td>@item.ID</td>
<td>@item.UserName</td>
<td>@item.UserPassword</td></tr>
}
}
</table>
</div>
</body>
</html>
同時為HomeControl內新增想要的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace StudyCSharp.Web.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public ActionResult Login()
{
ViewBag.Message = "Your login page.";
return View();
}
[HttpPost]
public ActionResult Login(string usernameL,string userpasswordL)
{
if (!ModelState.IsValid)
{
return View();
}
else
{
if(StudyCSharp.BLL.Userinformation_BLL.UserLogin(usernameL, userpasswordL))
{
ModelState.AddModelError("infoL", "登入成功");
return View("index");
}
else
{
ModelState.AddModelError("infoL", "使用者名稱或密碼錯誤");
return View("login");
}
}
}
[HttpPost]
public ActionResult Register(string usernameR, string userpasswordR)
{
if (!ModelState.IsValid)
{
return View("login");
}
else
{
StudyCSharp.Entity.UserInformation user = new Entity.UserInformation();
user.ID = 0;
user.UserName = usernameR;
user.UserPassword = userpasswordR;
if (StudyCSharp.BLL.Userinformation_BLL.CreateUserInfo(user) > 0)
{
ModelState.AddModelError("infoR", "註冊成功");
return View("login");
}
else
{
ModelState.AddModelError("infoR", "註冊失敗");
return View("login");
}
}
}
}
}