2012-05-24 66 views
0

我希望記錄刪除和編輯閱讀內容,並認爲最好的辦法是一個ActionFilter 屬性適用於我的刪除和編輯[文章]方法ActionFilter到RedirectToAction

但因爲最終結果是重定向到動作,我的Context.Result始終爲空 ,因爲只有一個Context.RedirectToAction結果可用。我去創造一些代碼插入我的刪除和編輯功能,有沒有人嘗試過這樣的事情!和你又能建議之前,現在

感謝

操作代碼:

[HttpPost, ValidateInput(false)] 
    [SiteChangeLogger(LogType = "Update", TableName = "Affiliates")] 
    public ActionResult Edit(Affiliate affiliate, FormCollection form) 
    { 
     var existing = db2.Affiliates.SingleOrDefault(x => x.AffiliateId == affiliate.AffiliateId); 
     ViewBag.before = Common.Strings.Base64Encode(Common.Strings.ToJsonString(existing)); 
     if (ModelState.IsValid) 
     { 
      try 
      { 
       var curFiles = new NameValueCollection(); 
       curFiles["AffiliateLogo"] = affiliate.AffiliateLogo; 
       if (!String.IsNullOrWhiteSpace(form["AffiliateLogo"])) 
       { 
        UploadFiles(form,curFiles); 
        TryUpdateModel(affiliate, form); 

        var oldFileName = affiliate.AffiliateLogo; 
        var newFileName = Common.Strings.RandomFileName(); 
        new WebImage(Server.MapPath("~/Content/images/" + affiliate.AffiliateLogo)) 
         .Resize(200, 50, true, true) 
         .Crop(1, 1) 
         .Save(Server.MapPath("~/Content/images/" + newFileName), "png", true); 
        affiliate.AffiliateLogo = newFileName + ".png"; 
        Common.Common.TryAndDeleteFile("~/Content/images/" + oldFileName); 
       } 
       else 
       { 
        affiliate.AffiliateLogo = existing.AffiliateLogo; 
       } 

      } 
      catch (Exception ex) 
      { 
       Common.Common.CompileErrorMessage(ex,"ADMIN.Affiliate.Edit"); 
      } 
      finally 
      { 
       db.Entry(affiliate).State = EntityState.Modified; 
       db.SaveChanges(); 
      } 
      ViewBag.after = Common.Strings.Base64Encode(Common.Strings.ToJsonString(affiliate)); 
      return RedirectToAction("Index"); 
     } 
     return View(affiliate); 
    } 

我濾膜代碼

public override void OnResultExecuted(ResultExecutedContext fc) 
    { 
     var viewResult = fc.Result as ViewResult; 

     if(viewResult == null) return; 

     var beforeData = viewResult.ViewBag.before; 
     var afterData = viewResult.ViewBag.after; 

     if (beforeData == null && afterData == null) return; 

     var ctx = new SgeGamesContext(); 
     var eventId = 0; 
     var siteChangeLogEvent = ctx.SiteChangeLogEvents.SingleOrDefault(x => x.SiteChangeLogEventName == LogType); 
     if (siteChangeLogEvent != null) 
     { 
      eventId = siteChangeLogEvent.SiteChangeLogEventId; 
     } 

     var model = new Sge.Games.Data.Models.SiteChangeLog 
         { 
          SiteChangeLogTable = TableName, 
          SiteId = 1, 
          SiteChangeLogAfterContent = afterData, 
          SiteChangeLogBeforeContent = beforeData, 
          SiteChangeLogEventId = eventId 
         }; 
     ctx.SiteChangeLogs.Add(model); 
     ctx.SaveChanges(); 

     base.OnResultExecuted(fc); 
    } 
+1

你應該表現出你的代碼,你嘗試過什麼? – archil

+0

代碼不適用它的空白,你自己創建代碼,你可以看到它在調試器中是空的,它是一個行爲的事情,我不從源代碼運行MVC。 – davethecoder

+0

您還可以查看代碼在這裏http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs – davethecoder

回答

2

你可以直接訪問ViewBag,你不需要的ViewResult:

public override void OnResultExecuted(ResultExecutedContext fc) 
{ 
    var before = fc.Controller.ViewBag.before; 
    var after = fc.Controller.ViewBag.after; 
    ... 
} 

Al所以您可能想使用OnActionExecuted事件而不是OnResultExecuted

+0

感謝達林,就是這樣,我知道有什麼: -)) 非常感謝 – davethecoder