zoukankan      html  css  js  c++  java
  • Asp.net MVC学习

    http://v.youku.com/v_playlist/f2416830o1p0.html
    一 工程结构
    4个程序集
    Microsoft.Web.Mvc --一些可以使用的,不确定的程序包
    System.Web.Mvc  --主程序库
    下面两个列入3.5的Net框架了
    System.Web.Abstractions --Request,Respose等5大对象、缓存,那些用于Asp.net Webfrom下使用的,这个使用装饰者模式来使的过去的使用的类,和mvc下使用的类像兼容。
    System.Web.Routing --

    同是3.5的引用
    System.Web.Extensions --Ajax控件

    文件
    App_Data 数据库
    Content  样式,图片,Js等
    Controllers 控制器,可以改变名字,默认值
    Models 模型
    Views  视图

    二 基本工作流程
    http://localhost:23797/home/index
    home 是控制器
    index 是Action
    HomeController --> Index()

    方法 return View(); 这个方法会默认访问View文件夹下的Home->Index.aspx文件
    方法 return View("about"); 访问我们指定的页面

    更改了代码,要重新生成一下,才会起作用

    三 MVC架构
    页面请求-->Controller->Models(ORM)->数据库
                  |
        页面 <--Views

    四 ViewData传值,本页View传递
       Controller --> Views 之间通过 ViewData 来传值。
       Controller中写 ViewData["zd"] = "欢迎你!";
       View中 调用 <%= ViewData["zd"] %>
      
       另外也可以是复杂类型
       List<string> sl = new List<string>();
       sl.Add("重典");
       sl.Add("冰动力");
       ViewData["zd"] = sl;
       View中调用
       <% foreach(string str in ViewData["zd"] as List<string>){ %>
       <%= str %>
       <% } %>

       对aspx页面,进行修改可以不编译生成。

    五 另外一种传值方式TempData,可以跨页面Action传递一次
       TempData["ddd"] = "哈哈";
       Response.Redirect("/home/about");
       页面about中
       <%= TempData["ddd"] %>
       只保留一次值。
      
       用途:比如程序运行到一定地方,报错误抛出异常了,到一个异常处理页面,把错误信息传过去,专门处理一下。

    六  ViewData的另外传递方式,类的传递
        定义一个类
        public class User
        {
     public string Name {get;set;}
     public int ID {get;set;}
        }
        在Controller中进行实例化
        User user = new User();
        user.ID = 1;
        user.Name = "安安";
       
        ViewData["user"] = user;
       
        在View中
        <%= (ViewData["user"] as User).Name %>

        还有一 更方便的方法:
        把对象放在 return View(user); 传递出来

        页面View
        首先修改一下页面class 继承
        比如:
        public partial class Index : ViewPage
        -->
        public partial class Index : ViewPage<User>
        然后再页面中
        <%= ViewData.Model.Name %>
        
        只能传递一个引用类型。
        将一个对象,用泛型类型传递到页面中。

    七  新建页面
        1. 新建一个Controller
           默认的Action是Index
           public ActionResult Index()
           {
               return View();
           }
         2. 新建一个View
            跟刚刚的Controller同名
    八  重定向,Redirect
        Response.Redirect("/user/edit");
        //WebFrom版通常跳转还是支持的

        新的return的MVC模式的跳转
        return Redirect("/user/edit");//新的跳转
        return RedirectToAction("edit");
        //同 控制器 不同Action 跳转
        return RedirectToAction("index","edit");
        //不同 控制器 跳转

    九  Url Routing路由
        home/index
        能定位到-->home控制器 -->index Action
        在golab.cs中

    八  filter 过滤器
        编码解码 数据压缩 服务设置协议 等等 都可以在这里做
        在Action发生之前 发生之后 执行
        在View显示之前 显示之后 执行
       
        新建一个类,起名加参数验证filter(ParamFiter)
        filter要继承过滤器的基类 System.Web.Mvc.ActionFilterAttribute
       
         重写方法
         protected override void OnActionExecuted(ActionExecutiongContext filterContext)
    {
       //Action运行之后
    }

         protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
       //Action运行之前
       if(filterContext.HttpContext.Request.QueryString["k"] != "goo")
        {
     throw new Exception("这有一个错误");
        }
    }

         protected override void OnResultExecuted(ResultExecutedContext filterContext)
    {
         //在View显示之后
    }
         protected override void OnResultExecuting(ResultExecutingContext filterContext)
    {
          //在View显示之前
    }

    filter过滤器怎么用
    在Controller中的类上面加上
    [ParamFilter]
    public class HomeControler : Controler
    整个Controller上都会运用这个过滤器

    也可以放在Controller中的某个Action上
    [ParamFilter]
    public ActionResult Index()

    http://localhsot:23797/?k=go 就可以访问了

    九 Helper初体验
       HtmlHelper 用来原有的服务器控件,显示数据的时候要借用Helper
       UrlHelper 由于url路由的存在,Url具有不确定性

       /home/index
       /home/index.ashx
       如果连接写死的话,就要整体替换,用urlhelper就可以避免。
       
       另外 Helper 只能在View页面上用,不要再控制器上使用,这个是规定不是绝对。
       HtmlHelper只要在View页面用Html调用就可以了。
       UrlHelper只要在View页面用url
       超链接
       <%= Html.ActionLink("首页","index","Home") %>
       HtmlHelper可以显示所有的表单元素,包含form都是用它来显示
       <%= Html.TextBox("category") %>
       HtmlHelper还有一个特殊的方法,输出编码作用
       <%= Html.Encode() %>
      
       UrlHelper只是用于显示URL的
       <%= Url.Action("index","home") %> 显示
       <%= Url.Content("//dd") %> 把网站路径转化为URL显示出来


    十  QueryString传值方式
        url?key1=value&key2=value2
        获取的时候就是用
        Request.QueryString["key1"]
       
        在Controller中,
        ViewDate["V"] = Request.QueryString["word"];
        在View中
        <%= ViewData["w"]%>

        在传值的调用页面中
        <%= Html.ActionLink("编辑页","edit","user",new {word = "zhongdian"},new {@class="x"}) %>
        最后一个属性是扩展的a标签的,这里给它一个样式。
        由于class是关键字,可以把Class(c大写)避免,也可以加@前导符转义。
        生成的Html页面代码
        <a href="/user/edit?word=zhongdian" class="x">编辑页</a>

        还有一个更简单的方法:
        在Controllers中改写
        public ActionResult Edit(string word)
        //作为Action的参数

    十一 表单提交Post
         <form> 因为提交的URL也有不确定因素,所以用Helper生成。
         创建form
         <% using(Html.Form("user","edit",FormMethod.Post)) { %>
          username:<%= Html.TextBox("Username") %>
          <br/>
          password:<%= Html.Password("Password") %>
          <br/>
          <%= Html.SubmitButton("","提交") %>
          <% } %>

          在Controller中接受Form表单的元素的值
          string un = Request.Form["Username"];
          string ps = Request.Form["Username"];
          ViewData["w"] = un + ps;
          在页面View中
          <%= ViewData["w"] %>

    十二 比较好的传值方式,UpdateModel
         UpdateModel其实是Controller集下的一个方法,这个方法是将Post、get提交过来的相应的值存储到一个对象。
         UpdateModel();
         定义类
         public class User
         {
           public string Name {get;set;}
           public string Password{get;set;}
         }
         在Controller中方法中如何写
         User u = new User(); //Model
         UpdateModel(u,Request.Form.AllKeys);//get也可以
         string un = u.Name;
         string ps = u.Password;
         ViewData["w"] = un + ps;
         在页面View中
         <%= ViewData["w"] %>

    十三 单选复选 更新
          单选框
         <%= Html.RadioButton("n1","man",false)%>性别
         单选框列表
         <%= foreash(string s in
    Html.RadioButtonList("ah",new[]{"音乐","书法"})
    )
    {%>
    <%= s %>
    <%}%>
         复选框
         <%= Html.CheckBox("c1")%> 复选

         在Controller中如何接受传过来的值
         ViewData["show"] = Request.Form["n1"]; //修改n1 为 ah 就可以测试显示列表
         在页面View中
         <%= ViewData["show"] %>

          在复选框的值有多个,而且name又是相同的话,可以用一个字符串数据接收它的值。
       
    十四 表单验证
    <form action="" method="post">
    <%= Html.ValidatiesMessage("u")%>
    <fieldset>
    <legend>提交用户</legend>
    <p><label>用户名</label>
    <%= Html.TextBox("u.UserName")%>
    </p>
    <p><label>密码</label>
    <%= Html.TextBox("u.Password")%>
    </p>
    <input type="submit"/>
    </fieldset>
    </form>

    后面Controller的代码
    HttpVerbs.Post
    public ActionResult Index(u as User)
    {
     if(u.UserName != "重典")
         ViewData.ModelState.AddModelError("u","用户名错误");
            if(u.Password != "123456")
                ViewData.ModelState.AddModelError("u","密码错");
            return View();
    }

  • 相关阅读:
    【Linux笔记】Linux目录结构
    《Effective C#》快速笔记(五)-
    《Effective C#》快速笔记(四)- 使用框架
    《Effective C#》快速笔记(三)- 使用 C# 表达设计
    《Effective C#》快速笔记(二)- .NET 资源托管
    《Effective C#》快速笔记(一)- C# 语言习惯
    Visual Studio 数据库架构比较
    C# 反射与dynamic最佳组合
    C# 调用WebApi
    基于微软开发平台构建和使用私有NuGet托管库
  • 原文地址:https://www.cnblogs.com/anan/p/1437231.html
Copyright © 2011-2022 走看看