zoukankan      html  css  js  c++  java
  • MVC4 学习笔记01

    1 、 ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别是什么?要注意什么吗? ActionResult 是一个抽象(abstract)类,ViewResult 只是ActionResult 的一个实现(implementation)。如果你确认你返回的是一个视图(view),你可以直接返回类型为ViewResult。ActionResult 有很多的派生类,如果你很确定你要返回的类型,你可以明确的返回该类型。如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回这些派生类的基类:ActionResult 。 ActionResult :http://msdn.microsoft.com/zh-cn/library/system.web.mvc.actionresult.aspx  ViewResult:http://msdn.microsoft.com/zh-cn/library/system.web.mvc.viewresult.aspx

    2、viewbag viewdata http://www.cnblogs.com/wintersun/archive/2012/01/21/2328563.html 本质:viewbag  是 dynamic(动态类型)  viewdata  是键值对

    3、 强类型视图(就是引用模型类)比弱类型视图多了 @model Partylnvites.Models.GuestResponse 还可以再创建视图的时候 使用自带的 create 、edit  模板,它会自动生成代码

    4、视图 页面 添加控件 ,@html.textboxfor 、Html.DropDownListFor  @using (Html.BeginForm())  编译之后变成 <form action="/Home/TestView" method="post">   5、 在 Homecontrol 代码文件 添加 模型引用 using Partylnvites.Models;  6、  return View()  view 里面有很多重载 在 control 方法 上夹   [HttpGet] [HttpPost] 区分了 form 的method,不同功能的视图结果 7、增加验证  在 模型类 增加using System.ComponentModel.DataAnnotations; 里面类的 的属性 上方 [Required(ErrorMessage = "Please enter your name")]  但还需要 在 方法里面判断if (ModelState.IsValid)   ,在 view 里面 的 form 里面 加上@Html.ValidationSummary()  才能 显示出  ErrorMessage 类容 ,编译之后 就是个div <div class="validation-summary-errors" data-valmsg-summary="true"><ul><li>Please enter your name</li>   </ul></div>         当你输入错误的时候 提交 返回的 input 标签变成了 <input class="input-validation-error" data-val="true" data-val-required=">Please enter your name" id="Name" name="Name" type="text" value="" /> 这为我们就好显示输入错误 文本框的样式

    8 、private set; public int ItemID { get; private set; } // The unique key     private set; 表示只读 http://bbs.csdn.net/topics/250016321

    9、MVC 模式  request-control-model-数据库-model-control-view-response

    10、  return View( view文件名(字符串),object)  object 一般会传入 一个model ,但这里定义object ,也可以传入 任意类型 比如说: (object)string.Format("Pname is {0}", names) 这里只传入 这个模型的用户名,那么 在 view 的代码怎么写呢?传入模型时 @model Partylnvites.Models.GuestResponse  ,现在只用户名了 所以 也可以 @model String  ,这就能理解view跟 控制器之间的传值关系了。

    11、Product myProduct = new Product { ProductID = 100, Name = "Kayak", Description = "A boat for one person", Price = 275M, Category = "Watersports" };  习惯像这样简写

    12、关于 IEnumerable      public class ShoppingCart02:IEnumerable<Product>     {         public List<Product> Products { get; set; }         public IEnumerator<Product> GetEnumerator()         {             return Products.GetEnumerator();         }

            IEnumerator IEnumerable.GetEnumerator()         {             return GetEnumerator();         }     } 因为 List<> 提供了 GetEnumerator 方法,所以这里可以直接用

    13、程序集 和 命名空间是多对多的关系 14、 扩展方法:   this 的使用 this 关键字 作为 扩展方法 ,扩展方法在静态类中声明,定义一个静态方法,为了区分扩展方法和一般的静态方法,扩展方法还需要给第一个参数使用this关键字,多个类可以使用相同的实现代码(初步认为是 当 这个类已经被封装了,没有源代码了,可以这样给它增加功能) public static class MyExtensionMethods { public static decimal TotalPrices(this ShoppingCart cartParam) { decimal total = 0; foreach (Product prod in cartParam.Products) { total += prod.Price; } return total; } } 15、扩展方法在接口中的使用     public class ShoppingCart02:IEnumerable<Product>     {         public List<Product> Products { get; set; }         public IEnumerator<Product> GetEnumerator()         {             return Products.GetEnumerator();         }

            IEnumerator IEnumerable.GetEnumerator()         {             return GetEnumerator();         }     }

    继承 IEnumerable 范型接口 在 一个 静态类中         public static decimal TotalPrices02(this IEnumerable<Product> Pro)         {             decimal Dtemp = 0;             foreach (Product prod in Pro)             {                 Dtemp += prod.Price;             }             return Dtemp;         } 在外部调用 IEnumerable<Product> pros = new ShoppingCart02()             {                 Products = new List<Product>{                    new Product(){                        Price=1                    }                }             };   pros.TotalPrices02(); 16、在过滤器中使用  扩展方法 (发现这是好方法!) 在 一个 静态类中         public static IEnumerable<Product> FilterByname(this IEnumerable<Product> Pro, string name)         {             foreach (Product P in Pro)             {                 if(P.Name==name){                     yield return P;                 }             }         } 在外部调用             IEnumerable<Product> Pros02 = new ShoppingCart02()             {                 Products = new List<Product>{                     new Product(){Name="s",Price=10},new Product(){Name="s",Price=20},new Product(){Name="ss",Price=11}                 }             };          IEnumerable<Product>  PS=     Pros02.FilterByname("s"); 使用罗曼达表达式  用委托 (我认为目的在于架构师提供 fill 功能,具体规则 程序员写) 在 一个 静态类中         public static IEnumerable<Product> Filter(this IEnumerable<Product> Pro, Func<Product, bool> selectorParam)         {             foreach (Product P in Pro)                {                 if (selectorParam(P))                     yield return P;             }         } 在外部调用             Func<Product, bool> Fundel = delegate(Product P)             {                 return P.Name == "a";             };             decimal Total = 0;             foreach ( Product P in PS.Filter(Fundel) ){                 Total += P.Price;             } 这里就不在局限于 过滤nane 属性了

    委托也可以简写为 罗曼达表达式  Func<Product, bool> Funlomanda = P => P.Name == "s"; 所以 也直接可以简写为  foreach (Product item in PS.Filter(P => P.Name == "s")) 17、使用Linq             var Result = Proarr.OrderByDescending(e => e.Price)                 .Take(1)                 .Select(e => new {                     e.Price, e.Name }); 按Price 排序 ,取 top 1

    18、注意  Product[] Proarr = { new Product() { Name = "bing", Price = 1 }, new Product() { Name = "CC", Price = 2 }, new Product() { Name = "CC", Price = 12 } };             var Result = Proarr.OrderByDescending(e => e.Price)                 .Take(1)                 .Select(e => new {                     e.Price, e.Name });// 执行完 上面代码 Result  结果 是 Name = "CC", Price = 12

                Proarr[2] = new Product() { Name = "bingt1", Price = 22 };//但是执行这个却会改变 Result 的结果  ,最后变成了Name = "bingt1", Price = 22

    但是  如果 不做  OrderByDescending 操作 ,改为   var Result = Proarr.Sum(e => e.Price); 最终Result  却不会发生改变 这个需要参照对照表  第 90 页 的Deferred

    19、使用异步 Using Async Methods   //没明白 using System.Net.Http; using System.Threading.Tasks; public class MyAsyncMethod     {         public static Task<long?> GetPageSize()         {             HttpClient client = new HttpClient();             var httpTask = client.GetAsync("http://apress.com");             return httpTask.ContinueWith((Task<HttpResponseMessage> antecedent) =>             {                 return antecedent.Result.Content.Headers.ContentLength;             });

            }

            public async static Task<long?> GetPageLength()         {

                HttpClient client = new HttpClient();             var httpMessage = await client.GetAsync("http://apress.com");             // we could do other things here while we are waiting             // for the HTTP request to complete             client.GetAsync("http://apress.com");             // we could do other things here while we are waiting             // for the HTTP request to complete             return httpMessage.Content.Headers.ContentLength;         }     }

    20、Razor 学习 使用强类型 @model  M 大写  与小写 是有区别的

    21、Layout 母版页  @RenderBody()  代表它的子页面

    22、Razor 视图 可以直接引用  @using Razor.Models

    23、Ninject  没明白 128 页 意思就是面向接口 http://www.cnblogs.com/haogj/archive/2013/05/01/3053171.html using Ninject;

    IKernel ninjectKernel = new StandardKernel();    实例化对象 ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();   绑定 接口和 类 IValueCalculator calc = ninjectKernel.Get<IValueCalculator>(); ShoppingCart cart = new ShoppingCart(calc) { Products = products }; decimal totalValue = cart.CalculateProductTotal(); return View(totalValue);

  • 相关阅读:
    linux下启动和关闭网卡命令及DHCP上网
    python 编码问题
    paddlepaddle
    Convolutional Neural Network Architectures for Matching Natural Language Sentences
    deep learning RNN
    Learning Structured Representation for Text Classification via Reinforcement Learning 学习笔记
    Python IO密集型任务、计算密集型任务,以及多线程、多进程
    EM 算法最好的解释
    tensorflow 调参过程
    tensorflow 学习纪录(持续更新)
  • 原文地址:https://www.cnblogs.com/bingguang/p/3821570.html
Copyright © 2011-2022 走看看