學習Asp.netMVC-牛刀小試(1)

      在上一篇的文章中,我們大概理解MVC 的模式后,我們應該是從基礎入手:

創(chuàng)新互聯(lián)公司專注于興海網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供興海營銷型網(wǎng)站建設,興海網(wǎng)站制作、興海網(wǎng)頁設計、興海網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務,打造興海網(wǎng)絡公司原創(chuàng)品牌,更為您提供興海網(wǎng)站排名全網(wǎng)營銷落地服務。

    1.我們先理解一下程序的入口 

    使用了URL重寫. ASP.NET中叫做UrlRouting,對應的程序集是System.Web.Routing, 打開項目的Global.asax.cs文件, 會找到我們建立的頁面重寫規(guī)則

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Users", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            System.Data.Entity.Database.SetInitializer(new MvcApplication1.Models.UsersInitializer());
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

       2.查找 Controller  

     

     在Controllers文件夾下我們可以找到UsersController.cs, 這里使用了一個約定, 就是如果URL中獲取到的Controller名字是Users, 則他的Controller類名就是UsersController. 在URL中的名字后加上”Controller”.

實例中Controller都放在Controllers文件夾, 所以我們可以按照命名約定很容易就可以找到HomeController類

       3.查找 Action 

一個Controller可以包含多個Action, MVC模式中Controller角色的具體實現(xiàn)邏輯都是在Action中的.

因為我們的Action是Index, 所以自然就要調(diào)用Index()方法.這里將"Webcome to ASP.NET MVC!",

private UsersDbContext db = new UsersDbContext();

        //
        // GET: /Users/

        public ViewResult Index()
        {
            return View(db.Users.ToList());
        }

        //
        // GET: /Users/Details/5

        public ViewResult Details(int id)
        {
            Users users = db.Users.Find(id);
            return View(users);
        }

        //
        // GET: /Users/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Users/Create

        [HttpPost]
        public ActionResult Create(Users users)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(users);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(users);
        }
        
        //
        // GET: /Users/Edit/5
 
        public ActionResult Edit(int id)
        {
            Users users = db.Users.Find(id);
            return View(users);
        }

        //
        // POST: /Users/Edit/5

        [HttpPost]
        public ActionResult Edit(Users users)
        {
            if (ModelState.IsValid)
            {
                db.Entry(users).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(users);
        }

        //
        // GET: /Users/Delete/5
 
        public ActionResult Delete(int id)
        {
            Users users = db.Users.Find(id);
            return View(users);
        }

        //
        // POST: /Users/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Users users = db.Users.Find(id);
            db.Users.Remove(users);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

       4.查找view 視圖 

      View方法中可以帶一個名字, 這個名字就是View的名字.如果把index改成Edit,那么訪問/Users/Edit就會跳轉(zhuǎn)到Edit頁!

           5. 頁面展示

             接下來ViewEngine即頁面引擎會將aspx中的HTML部分以及上面的數(shù)據(jù)部分和在一起返回給瀏覽器.

關于View對象我注意到此頁面是繼承自System.Web.Mvc.ViewPage而不是直接繼承自System.Web.UI.Page, 而這個ViewData對象就是ViewPage中的一個屬性. 這里的ViewData一定是頁面級別的,當頁面編譯完畢這個對象就會被注銷(HTTP是無狀態(tài)的協(xié)議,每次請求其實都是生成一個新的ViewPage對象).

@model IEnumerable<MvcApplication1.Models.Users>

@{
    ViewBag.Title = "Index";
}

<h3>Index</h3>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            UserID
        </th>
        <th>
            UserName
        </th>
        <th>
            Password
        </th>
        <th>
            RegTime
        </th>
        <th>
            IsTest
        </th>
        <th>
            IsEnable
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UserID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RegTime)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsTest)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsEnable)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

效果圖:

學習Asp.net MVC-牛刀小試(1)


文章題目:學習Asp.netMVC-牛刀小試(1)
標題URL:http://muchs.cn/article22/pihejc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、Google、云服務器網(wǎng)站維護、全網(wǎng)營銷推廣、品牌網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設