码迷,mamicode.com
首页 > Web开发 > 详细

MVC 5学习总结笔记1

时间:2017-01-15 18:05:29      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:first   ext   require   end   other   href   get   row   数据传递   

01.使用MVC自带的DataAnnotations实现数据验证

技术分享
public class ExternalLoginConfirmationViewModel
{
    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }
}
View Code

自定义数据验证功能(用法及定义):

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MVC5.Models
{
    public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }

        [FirstNameValidation]
        public string FirstName { get; set; }
        [StringLength(5,ErrorMessage="Last name length should not more than 5!")]
        public string LastName { get; set; }
        public int Salary { get; set; }
    }
}
View Code
技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MVC5.Models
{
    public class FirstNameValidation:ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                return new ValidationResult("Please input FirstName");
            }
            else
            {
                if (value.ToString().Contains("@"))
                {
                    return new ValidationResult("The first name should not contains @!");
                }
            }
            return ValidationResult.Success;
        }
    }
}
View Code

02.Model Binder

在.NET的核心MVC中,Model Binder用于映射从HTTP请求到Action方法的参数,可以参考链接

创建自定义 Model Binder ,代替默认的Model Binder.

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC5.Models
{
    public class MyEmployeeModelBinder:DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext,ModelBindingContext bingingContext,Type modelType)
        {
            Employee emp = new Employee();
            emp.FirstName = controllerContext.RequestContext.HttpContext.Request["FName"].ToString();
            emp.LastName = controllerContext.RequestContext.HttpContext.Request["LName"].ToString();
            emp.Salary = int.Parse(controllerContext.RequestContext.HttpContext.Request["Salary"].ToString());
            return emp;
        }
    }
}
View Code

03. Action的特性:

[NonAction]表示将该Action作为方法使用;

[ChildActionOnly]表示它只能在前台View中通过Html.Action或Html.RenderAction来使用,不能通过在地址栏输入地址直接访问;

[ActionName]表示为某个Action重新指定一个新的名字来使用,如下所示,必须使用新名称来进行调用,即:@Url.Action("GetActionNameByNewName", "Patient")‘。

技术分享
/// <summary>
/// Testing for ActionName
/// </summary>
/// <returns></returns>
[ActionName("GetActionNameByNewName")]
public ActionResult GetActionNameByOldName()
{
     return xxx;
}
View Code

04. Razor代码带花括号和没有花括号的区别:

    @符号后没有花括号只是简单的显示变量或表达式的值,如:

  <li><a href=@Menu.URL>@Menu.DisplayName</a></li>;

    @符号后有花括号则表明是对服务器代码的执行,如:

  @{

         var ParentMenu = Model.Where(x => x.ParentId == 0);

     }

05. 认证属性:[Authorize]

为了确保每个Action方法在执行前都通过当前系统的登录验证,则需要在每个要验证Action的方法前加上[Authorize];

06. FormsAuthentication.SetAuthCookie(string userName,bool createPersistentCookie)

作用:将当前已通过验证的用户的登录名加密后放入Cookie中;

参数分析:

userName :The name of an authenticated user. This does not have to map to a Windows account.

译:已通过验证的用户的登录名,和Windows账户没有强制的对应关系;

createPersistentCookie :true to create a persistent cookie (one that is saved across browser sessions); otherwise, false.

译:是否要对Cookie进行永久保存?

07. MVC过滤器:

可以参考一下对过滤器的详细介绍:示例

08. @model与@Model的区别:

技术分享

09. 常见接口:

HttpContext:获取客户端向服务端请求的相关信息,对Request/Response/Server/Session等进行了封装;

总结: A. ViewBag实质还是在内部调用ViewData;

         B. ViewModel是用于Model与View之间进行数据传递的;

     C.  RouteTable:存储URL;

MVC 5学习总结笔记1

标签:first   ext   require   end   other   href   get   row   数据传递   

原文地址:http://www.cnblogs.com/sccd/p/5966344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!