新聞中心
我們這里將要談到的是ASP.NET MVC PRG數(shù)據(jù)驗(yàn)證,主要是參考一些國(guó)外關(guān)于PRG數(shù)據(jù)驗(yàn)證的文章,希望對(duì)大家有所幫助。

為麻栗坡等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及麻栗坡網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站建設(shè)、麻栗坡網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
我的理念:
既然是ASP.NET MVC,那就肯定要用PRG。但是簡(jiǎn)單的PRG不能在輸入頁(yè)面顯示Html.ValidationMessage,另一個(gè)就是之前的數(shù)據(jù)會(huì)被全部清空或者初始化了。
想想要我是打了半天的字一下全沒了那多慘啊。你的訪客不氣傻了才怪。
OK,Google一下,找到了http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx
阿,他叫什么名字我不認(rèn)識(shí),我也看不懂英文的版權(quán)聲明,所以這只有個(gè)鏈接沒署名了。誰(shuí)認(rèn)識(shí)他叫他寫個(gè)C#版或者VB.NET版的版權(quán)聲明吧,謝謝。
英文不好不要緊,直接看第13點(diǎn):Use PRG Pattern for Data Modification
- Controller
- [AcceptVerbs(HttpVerbs.Get), OutputCache(CacheProfile = "Dashboard"), StoryListFilter, ImportModelStateFromTempData]
- public ActionResult Dashboard(string userName, StoryListTab tab, OrderBy orderBy, int? page)
- {
- //Other Codes
- return View();
- }
- [AcceptVerbs(HttpVerbs.Post), ExportModelStateToTempData]
- public ActionResult Submit(string userName, string url)
- {
- if (ValidateSubmit(url))
- {
- try
- {
- _storyService.Submit(userName, url);
- }
- catch (Exception e)
- {
- ModelState.AddModelError(ModelStateException, e);
- }
- }
- return Redirect(Url.Dashboard());
- }
自定義了兩個(gè)ActionFilter,阿,作者好像打錯(cuò)別字了。您別在意。
- ModelStateTempDataTransfer
- public abstract class ModelStateTempDataTransfer : ActionFilterAttribute
- {
- protected static readonly string Key = typeof(ModelStateTempDataTransfer).FullName;
- }
- public class ExportModelStateToTempData : ModelStateTempDataTransfer
- {
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- //Only export when ModelState is not valid
- if (!filterContext.Controller.ViewData.ModelState.IsValid)
- {
- //Export if we are redirecting
- if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult))
- {
- filterContext.Controller.TempData[Key] = filterContext.Controller.ViewData.ModelState;
- }
- }
- base.OnActionExecuted(filterContext);
- }
- }
- public class ImportModelStateFromTempData : ModelStateTempDataTransfer
- {
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary;
- if (modelState != null)
- {
- //Only Import if we are viewing
- if (filterContext.Result is ViewResult)
- {
- filterContext.Controller.ViewData.ModelState.Merge(modelState);
- }
- else
- {
- //Otherwise remove it.
- filterContext.Controller.TempData.Remove(Key);
- }
- }
- base.OnActionExecuted(filterContext);
- }
- }
因?yàn)槲矣玫氖荲B.NET,直接拿工具轉(zhuǎn)了放自己項(xiàng)目里,英文不懂照他的源代碼套上去一試。
哈,成功了,可愛的Html.ValidationMessage來(lái)了。
但是還是沒有解決數(shù)據(jù)被清空或初始化的問(wèn)題。
這下慘了,Google也好,百度也好,還是在博客園里找這找那都沒找著。我估計(jì)可能我找的方法不對(duì)吧,不然這么多人都碰到的問(wèn)題怎么沒人發(fā)出來(lái)呢。
最后在園子的小組里找到http://space.cnblogs.com/group/topic/7919/ 第三個(gè)回復(fù)有說(shuō)ModelState.SetModelValue方法,拿過(guò)來(lái)一試,真不錯(cuò)。
所以修改了一下剛才所說(shuō)的兩個(gè)ActionFilter中的ExportModelStateToTempData;代碼如下:
- Code
- Public Overrides Sub OnActionExecuted(ByVal pFilterContext As ActionExecutedContext)
- If Not pFilterContext.Controller.ViewData.ModelState.IsValid Then
- If TypeOf (pFilterContext.Result) Is RedirectResult OrElse TypeOf (pFilterContext.Result) Is RedirectToRouteResult Then
- If pFilterContext.HttpContext.Request.Form.Count > 0 Then
- Dim tFormCollection As New FormCollection(pFilterContext.HttpContext.Request.Form)
- For Each fKey As String In tFormCollection
- pFilterContext.Controller.ViewData.ModelState.SetModelValue(fKey, tFormCollection.ToValueProvider(fKey))
- Next
- pFilterContext.Controller.TempData(mKey) = pFilterContext.Controller.ViewData.ModelState
- End If
- End If
- End If
- MyBase.OnActionExecuted(pFilterContext)
- End Sub
最后說(shuō)下,因?yàn)槲矣玫腣B.Net是不區(qū)分大小寫的,所以我的每個(gè)參數(shù)都用p開頭,臨時(shí)變量用t開頭,內(nèi)部循環(huán)用f開頭,客官將就著看吧。
文章標(biāo)題:詳解ASP.NET MVC PRG數(shù)據(jù)驗(yàn)證
本文URL:http://www.dlmjj.cn/article/cooscoo.html


咨詢
建站咨詢
