標籤:http io os ar java for sp 資料 2014
---------------------------------------------------------------------15:28 2014/7/21
強型別化
後台
ViewData.Model=userInfo;//userInfo對象傳進去。
前台
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcUserDemo.Models.UserInfo>"%>
<%: Html.TextBoxFor(u=>u.UserName)%>
編碼化
<%= ViewData["strScript"]%> //不編碼
<%: Html.Raw("<p>我是HtmlRaw</p>")%> //不編碼
<%: new HtmlString("<p>不編碼</p>")%> //不編碼
<%: new MvcHtmlString("<p>不編碼</p>")%> //不編碼
<%: ViewData["strScript"]%> //編碼
<%: Html.EnCode(ViewData["strScript"])%> //編碼
擴充方法
擴充方法三個要素:靜態類,靜態方法,this關鍵字
namespace System.Web.Mvc
public static class MyHtmlHelperExt
{
public static string MyLabel(this HtmlHelper helper,string txt)
{
return string.Format("<span>{0}</span>",txt);
}
public static HtmlString MyHtmlStringLabel(this HtmlHelper helper,string txt)
{
return new HtmlString( string.Format("<span>{0}</span>",txt));
}
}
jquery確認提示
$(function(){
$("a:contains(‘刪除‘)").click(function(){
return confirm("請確認是否真的刪除資料?");
});
})
Razor引擎的轉換資料類型
AsInt(),IsInt(),AsFloat(),IsFloat(),AsDecimal(),IsDecimal(),AsDateTime(),IsDateTime(),AsBool(),IsBool(),ToString()
ActionResult衍生類別
路由調試設定代碼
Application_Start()
RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
------------------------------------------------------------------------------10:39 2014/7/22--------
MVC驗證
System.ComponentModel.DataAnnotations中的ValidationAttribute基類,定義完全定製的特性
[Required][StringLength][Range][RegularExpression]
伺服器端Action中校正,Model.IsValidate為true即可。
用戶端引入jq校正<% Html.EnableClientValidation();%>
Webconfig中可以設定全域用戶端校正是否開啟或關閉。
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
微軟提供了預設的過濾器
Authorization filter,Action filter,Result filter,Exception filter
關閉xss
<httpRuntime requestValidationMode="2.0" />
asp.net MVC 筆記