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(,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 == )
{
<li>@Html.ActionLink(item.DisplayName, "List", item.URL, IsAreaProject ? null : new { area = item.ParentId }, null)</li>
} if (subChildCount > )
{
<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;

												

最新文章

  1. 软件工程 Android小游戏 猜拳大战
  2. Ubuntu 16.04 Steam
  3. [其他]Ubuntu安装genymotion后unable to load VirtualBox engine
  4. python 代码片段17
  5. nginx模块开发(31)—定时器模型
  6. Linux/Unix笔记本
  7. android 入门 003 (点击事件)
  8. Win7/8下提示OpenSCManager failed 拒绝访问
  9. [转]从两道经典试题谈C/C++中联合体(union)的使用
  10. 11.1 morning
  11. zoj1013 Great Equipment
  12. 解决Charles Response 中文乱码
  13. Oracle学习笔记之游标详解
  14. vi检索
  15. vue层级关系的数据管理
  16. 台式电脑、笔记本快捷选择启动项Boot 快捷键大全
  17. netty基础
  18. javaweb环境搭建
  19. HTTP API网关选择之一Kong介绍
  20. TCP的窗口滑动机制

热门文章

  1. GameUnity 2.0 文档(四) 网格+四叉树 最优碰撞检测
  2. a:link visited hover active
  3. 2016.10.08--Intel Code Challenge Final Round--D. Dense Subsequence
  4. hdu_3886_Final Kichiku “Lanlanshu”(数位DP)
  5. Linux添加系统环境变量的两种方法
  6. 转:sqlplus与shell互相传值的几种情况
  7. maven项目如何跳过某些junit test,或者指定执行部分junit test
  8. 修改sqlserver的数据库排序规则语句
  9. ZOJ 3939The Lucky Week&lt;模拟/暴力&gt;
  10. C#获取IPv4代码