01.使用MVC自带的DataAnnotations实现数据验证
public class ExternalLoginConfirmationViewModel { [Required] [Display(Name = "Email")] public string Email { get; set; } }
自定义数据验证功能(用法及定义):
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; } } }
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; } } }
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; } } }
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; }
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. Partial定义方法并调用:
<ul class="dropdown-menu"> @GenerateMenuList(item.Id) </ul> @helper GenerateMenuList(int id) { var subMenu = Model.RoleFunction.Where(x => x.ParentId == id).OrderBy(x => x.Ordering); foreach (NCGH.UI.Common.ViewModels.RoleFunctionViewModel item in subMenu) { var subChildCount = Model.RoleFunction.Where(x => x.ParentId == item.Id).Count(); if (subChildCount == 0) { <li>@Html.ActionLink(item.DisplayName, "List", item.URL, IsAreaProject ? null : new { area = item.ParentId }, null)</li> } if (subChildCount > 0) { <li class="dropdown-submenu"> <a tabindex="-1" href="#">@item.DisplayName</a> <ul class="dropdown-menu"> @GenerateMenuList(item.Id) </ul> </li> } } }
10. 常见接口:
HttpContext:获取客户端向服务端请求的相关信息,对Request/Response/Server/Session等进行了封装;
总结: A. ViewBag实质还是在内部调用ViewData;
B. ViewModel是用于Model与View之间进行数据传递的;
C. RouteTable:存储URL;