標籤:右鍵 append string dia lis model 密碼 users index
建立--項目--ASP.NET MVC 4 WEB 應用程式
MVC:
M--Model 模型層 放置資料訪問類,linq
V--View 視圖層 介面層 aspx檔案。只有一個分頁檔,沒有C#代碼檔案 ,那怎麼執行?
<%@ %>引用命名空間
<% %>在此地區裡嵌所有C#代碼
<%@ %>輸出一個變數的值
C--Control 控制層 添加-控制器--
C層:由控制器及控制器中的動作所組成,MVC請求不是直接對頁面,而是對控制器中的某個動作發送請求
預設返回 Home控制器下的Index動作
如果有請求的控制器,沒有請求動作。那麼會預設返回
namespace mvc1.Controllers{ public class HomeController : Controller { //動作 public string Index() { return "hole word"; } public string Other1() { return "<html><head></head><body><h1>"+DateTime.Now+"<h1></body></html>"; } }}
qidian1Controller.cs:
namespace mvc1.Controllers{ public class qidianController : Controller { public string Index() { StringBuilder ss = new StringBuilder(); using (DataClasses1DataContext con = new DataClasses1DataContext()) { List<Users> ulist = con.Users.ToList(); foreach(Users u in ulist) { ss.Append(u.NickName+"|");//追加 } } return ss.ToString(); } public ActionResult other1()//添加視圖--右鍵第一個(會出現一個html:other1.aspx) { return View(); }
other1.aspx:
<%@ Import Namespace="mvc1.Models" %> <%--引用命名空間--%><!DOCTYPE html><html><head runat="server"> <meta name="viewport" content="width=device-width" /> <title>other1</title></head><body> <div> <h1>這是視圖產生出的頁面</h1> <table> <tr> <td>使用者名稱</td> <td>密碼</td> <td>暱稱</td> <td>性別</td> <td>生日</td> <td>民族</td> </tr> <% List<Users> ulist = new UsersData().Select();//在model裡添加一個類,來這調用 foreach(Users u in ulist) { %> <tr> <td><%=u.UserName %></td> <td><%=u.Password %></td> <td><%=u.NickName %></td> <td><%=Convert.ToBoolean(u.Sex)?"男":"女" %></td> <td><%=u.Birthday.Value.ToString("yyyy年MM月dd日") %></td> <td><%=u.Nation1.NationName %></td> </tr> <% } %> </table> </div>
UsersData類:
namespace mvc1.Models{ public class UsersData { DataClasses1DataContext con = null; public UsersData() { con = new DataClasses1DataContext(); } public List<Users> Select() { return con.Users.ToList(); } }
MVC開發基礎