如何在MVC中使用chain-pages方法进行验证
YPE html>
æ们å¨ç¨åºå¼åçæ¶ååªæ¯æ¹åäºç¼å代ç çæ¹å¼ï¼æ们è¦å¨è¿æ¹é¢å¦ä½æ交ç¼ç æçå¢ï¼æ¥ä¸æ¥ï¼ç±ç«ææ¯é¢éå°ç¼åè¯å¤§å®¶å¦ä½å¨MVCä¸ä½¿ç¨chain-pagesæ¹æ³è¿è¡éªè¯ï¼ä¸èµ·æ¥äºè§£ä¸ä¸å§ï¼
.Net 3.0 æ·»å äºä¸ä¸ªè¯æ³ç³å°±æ¯æ©å±æ¹æ³ï¼å ¶å®æ©å±æ¹æ³çæ¬è´¨å°±æ¯å¯¹ç±»çéææ¹æ³çè°ç¨ï¼è½ç¶æ©å±æ¹æ³åªæ¯æ¹åäºæ们å代ç çæ¹å¼ï¼ä½æ¯å¦ææ们使ç¨å¾å½ï¼å¯ä»¥ç»æ们带æ¥å·¨å¤§çç¼ç æççæåã对äºC#æ©å±æ¹æ³ç使ç¨ï¼æå°±ä¸ç»è¯´äºï¼è´´æ®µä»£ç 说ææ©å±æ¹æ³ç使ç¨ï¼å¤§å®¶å°±ä¼åå¿èµ·æ¥ã
public static class Extensions
{
public static string EndWith(this string str)
{
return str + "@";
}
}
对äºè¿æ®µé常ç®åç代ç ï¼ç¸ä¿¡å¤§å®¶åºè¯¥ä¼å¾æç½ï¼å¯ä»¥çå°æ©å±æ¹æ³ç使ç¨ï¼å°±æ¯å¨ä¸ä¸ªéæç±»ä¸å®ä¹ä¸ä¸ªéææ¹æ³ï¼å¨æ¹æ³åæ°ä¸æ·»å thisã
é£ä¹ä½¿ç¨æ©å±æ¹æ³å°±æ´å çç®åäºã以ä¸ä»£ç ç段就æ¯æ©å±æ¹æ³å¯¹äºstringçæ©å±ï¼ä¼å¨é¡µé¢ä¸æ¾ç¤º@
Â
Â
string str = "";
Response.Write(str.EndWith());
æ©å±æ¹æ³ä¸æ¯æ¬æ¬¡è¯¾é¢çéç¹ï¼æ¬æ¬¡è¯¾é¢çéç¹æ¯è¯´æå¦ä½å©ç¨æ©å±æ¹æ³æ¥é¾å¼ç对MVC 3ä¸ç页é¢è¿è¡éªè¯ã对äºMvcçéªè¯ï¼æå¾å¤ç§æ¹å¼ï¼å
¶ä¸æ们ç»å¸¸ä½¿ç¨çå°±æ¯Modelçéªè¯ï¼è¿æ¯æç´æ¥æææçæ们æä½çæ¹å¼ï¼å¯ä»¥å¯¹ä¸ä¸ªå段è¿è¡å¤ä¸ªæ¡ä»¶çéªè¯ãä½æ¯è¿ç§éªè¯æ¹å¼æä¸ä¸ªå¼ç«¯ï¼å°±æ¯æ们ä¸è½å¨é¡µé¢ä¸ç´æ¥çå°å¯¹è¯¥å段è¿è¡äºåªäºéªè¯ï¼é¤éæ们å»Modelå±æ¥çï¼ä½æ¯é常ä¸ä¸ªé¡¹ç®æ¯å项ç®ç»çï¼æ们ä¸ä¸å®è´è´£å¼åModelå±ï¼æ以è¿å°±ä¼éå°ä¸ä¸ªæ²éçé®é¢ãæ们é½ç¥éï¼ç¨åºåçæ²éææ¬æ¯å¾é«çã
ç°å¨æä¾å¦ä¸ç§è¿è¡é¡µé¢éªè¯çæ¹å¼ï¼è¿ç§æ¹å¼ä¹å¯ä»¥å¯¹ä¸ä¸ªé¡µé¢å
ç´ è¿è¡å¤ä¸ªæ¡ä»¶çéªè¯ï¼å¹¶ä¸æ¯å¨å®¢æ·ç«¯è¿è¡çãè¦ä½¿ç¨è¿ä¸ªéªè¯ï¼æ们å¿
é¡»äºè§£ä¸ä¸ªjqueryçæ件ï¼é£å°±æ¯jquery.validation.jsæ件ï¼è¿ä¸ªæ¯å¯¹å
ç´ è¿è¡éªè¯çjsæ件ã
è¿æä¸ä¸ªæ们å¿
é¡»äºè§£çç¥è¯ç¹å°±æ¯ï¼Mvc 3çRazor è§å¾ç´æ¥ç»§æ¿èªåªä¸ªç±»ï¼ç¸ä¿¡å¾å¤æåé½ä¼æ¯«ä¸ç¹è±«ç说æ¯ViewPageï¼å½ç¶è¿ä¸è½è¯´æ¯éçï¼å 为Mvc 2 ç¡®å®æ¯ç»§æ¿èªViewPageåºç±»ï¼ä½æ¯å¨Razor è§å¾ä¸ï¼æ¯ç»§æ¿èªWebViewPageç±»çï¼å¦æä½ çæ©å±æ¹æ³æ©å±çæ¯ViewPageï¼é£ä¹å¯¹ä¸èµ·ï¼ä½ å¨é¡µé¢ä¸æ¯æ æ³çå°è¿ä¸ªæ©å±æ¹æ³çï¼é¤é继æ¿èªWebViewPageãå¸æ大家æ©å±é¡µé¢æ¹æ³çæ¶å注æä¸ä¸ç±»ç继æ¿é®é¢ã
说äºåå¤å·¥ä½ï¼é£ä¹æ们åå
¥æ£é¢ï¼å
è´´åºæ¥ä»£ç ï¼ç¶åæ们åéæ¸çåæã
Â
Â
namespace MvcWeb.Extensions
{
public class JqueryHelper
{
public ViewContext ViewContext { get; private set; }
public WebViewPage ViewPage { get; private set; }
public RouteCollection RouteCollection { get; private set; }
public JqueryHelper(ViewContext viewContext, WebViewPage page, RouteCollection routeCollection)
{
this.ViewContext = viewContext;
this.ViewPage = page;
this.RouteCollection = routeCollection;
}
public JqueryHelper(ViewContext viewContext, WebViewPage viewPage) : this(viewContext, viewPage, RouteTable.Routes) { }
}
public static class JqueryExtensions
{
public static JqueryHelper Jquery(this WebViewPage page)
{
var key = typeof(JqueryHelper);
var jquery = page.PageData[key] as JqueryHelper;
if (jquery == null)
{
page.PageData[key] = jquery = new JqueryHelper(page.ViewContext, page);
}
return jquery;
}
}
public class JqueryValidations
{
public WebViewPage Page { get; private set; }
public JqueryValidations(WebViewPage page)
{
this.Page = page;
}
private Dictionary
private Dictionary
public void Required(string name,string message)
{
this.AddRuleAndMessage(name, "required", true, message);
}
public void Email(string name, string message)
{
this.AddRuleAndMessage(name,"email",true,message);
}
public void Number(string name, string message)
{
this.AddRuleAndMessage(name, "number", true, message);
}
public void Range(string name,int min,int max, string message)
{
this.AddRuleAndMessage(name, "range", new int[] { min, max }, message);
}
public string ToScripts(string form)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
StringBuilder builder = new StringBuilder();
builder.Append("$(");
serializer.Serialize(form, builder);
builder.Append(").validate(");
serializer.Serialize(
new
{
rules = this.m_rules,
messages = this.m_message,
onkeyup = false
}, builder);
builder.Append(");");
return builder.ToString();
}
public ValidationElement Element(string name)
{
return new ValidationElement(name, this);
}
private void AddRuleAndMessage(string name, string rule, object value, string message)
{
if (!this.m_rules.ContainsKey(name))
{
this.m_rules[name] = new Dictionary
}
this.m_rules[name][rule] = value;
if (!string.IsNullOrEmpty(message))
{
if (!this.m_message.ContainsKey(name))
{
this.m_message[name] = new Dictionary
}
this.m_message[name][rule] = message;
}
}
}
public class ValidationElement
{
public ValidationElement(string name, JqueryValidations validations)
{
this.Name = name;
this.Validations = validations;
}
public string Name { get; private set; }
public JqueryValidations Validations { get; private set; }
public ValidationElement Required(string message)
{
this.Validations.Required(this.Name, message);
return this;
}
public ValidationElement Email(string message)
{
this.Validations.Email(this.Name, message);
return this;
}
public ValidationElement Number(string message)
{
this.Validations.Number(this.Name, message);
return this;
}
public ValidationElement Range(int min, int max, string message)
{
this.Validations.Range(this.Name, min, max, message);
return this;
}
}
public static class JQueryValidationExtensions
{
public static JqueryValidations Validations(this JqueryHelper jquery)
{
return jquery.Validations("(default)");
}
public static JqueryValidations Validations(this JqueryHelper jquery, string name)
{
var key = typeof(JqueryValidations) + "+" + name;
var page = jquery.ViewPage;
var validations = page.PageData[key] as JqueryValidations;
if (validations == null)
{
page.PageData[key] = validations = new JqueryValidations(page);
}
return validations;
}
}
}
å¨è¿æ®µä»£ç ä¸å®ä¹äºJqueryHelper ç±»ï¼æ¯å¯¹WebViewPageçæ©å±ï¼å
¶ä¸æä¸ä¸ªæ¹æ³ToScript æ们è¦ç¹å«æ³¨æä¸ä¸ï¼è¿ä¸ªæ¹æ³æ¯å¨é¡µé¢ä¸æ¾ç¤ºä¸æ®µæ¼æ¥çjsï¼ä¹å°±æ¯jquery.validation.jsä¼ç¨å°çéªè¯é¡µé¢å
ç´ çjsã
å
·ä½ä»£ç æå°±ä¸ç»è¯´äºï¼æä»ç»ä¸ä¸åºæ¬ææ³å§ãé¦å
æ们å®ä¹äºä¸ä¸ªJqueryHelperç±»ï¼ç¶å顶ä¸ä¸ªJqueryValidationç±»æ¥å®ä¹å¯¹é¡µé¢å
ç´ å¯ä»¥è¿è¡çéªè¯ãè¿éé¢æä¸ä¸ªé¾å¼æ¹æ³å¿
é¡»ç¨å°çå°±æ¯æ¯ä¸ªéªè¯æ¹æ³è¿åçç»æé½æ¯JqueryValidationç±»ç对象ï¼è¿æ ·æ们æå¯ä»¥è¿è¡é¾å¼æä½ã
éè¿Dictionaryæ¥åå¨éªè¯çæ ç¾Nameå±æ§å¼ï¼é误信æ¯ï¼ä»¥åéªè¯è§åï¼æåæ ¹æ®è¿äºå¼å¨ToScriptä¸æ¼æ¥åºå¯ä»¥æ交çjsã
好äºï¼æ¥ç个ç»æï¼å¤§å®¶å°±ä¼æ´å æ¸
æ¥è¿ä¸ªé¾å¼æ¹æ³çç¨éæå¨äºã
Â
Â
ä¸é¢æ¯ç±ç«ææ¯é¢éå°ç¼ä¸ºå¤§å®¶ä»ç»çå¦ä½å¨MVCä¸ä½¿ç¨chain-pagesæ¹æ³è¿è¡éªè¯ï¼æ们åä¸ä¸è¦æ¿èªå·±ç项ç®åé便çæä½ã
上一篇:.NET 中的装箱与拆箱实现过程