MVC開發基礎

來源:互聯網
上載者:User

標籤:右鍵   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開發基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.