温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

学习Asp.net MVC-牛刀小试(1)

发布时间:2020-07-14 02:07:20 来源:网络 阅读:634 作者:海莉zhe 栏目:编程语言

      在上一篇的文章中,我们大概理解MVC 的模式后,我们应该是从基础入手:

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

    使用了URL重写. ASP.NET中叫做UrlRouting,对应的程序集是System.Web.Routing, 打开项目的Global.asax.cs文件, 会找到我们建立的页面重写规则

 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角色的具体实现逻辑都是在Action中的.

因为我们的Action是Index, 所以自然就要调用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就会跳转到Edit页!

           5. 页面展示

             接下来ViewEngine即页面引擎会将aspx中的HTML部分以及上面的数据部分和在一起返回给浏览器.

关于View对象我注意到此页面是继承自System.Web.Mvc.ViewPage而不是直接继承自System.Web.UI.Page, 而这个ViewData对象就是ViewPage中的一个属性. 这里的ViewData一定是页面级别的,当页面编译完毕这个对象就会被注销(HTTP是无状态的协议,每次请求其实都是生成一个新的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)



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI