2013-09-22 31 views
0

大家好,你能告訴我還在學習,我不知道如何或什麼是爲了使用重用我的代碼的數據庫操作的最佳方法;例如下面MVC3我怎麼能重用我的ActionResult代碼

代碼
// i reuse this code multiple times throughout my site but everytime I change it I must change 
// all of the different Edit's each time I would like a central hub for all of it. 
[Authorize] 
    public ActionResult Edit() 
    { 



     var ss = User.Identity.Name; 
     int myid = Convert.ToInt32(ss); 
     var buyer = (from s in db.buyers where myid == s.RegistrationID select s).FirstOrDefault(); 


     ViewBag.RegistrationID = myid; 

     if (buyer != null && buyer.RegistrationID == myid) 
     { 

      return View(buyer); 
     } 
     else 
     { 
      RedirectToAction("buyerrequire"); 

     } 
     return View("buyerrequire"); 
    } 

如何將這些代碼放入可重複使用的容器中?所以如果我改變了那裏的東西,它會改變整個網站使用那個容器,整理像一個_部分除了ActionResults ...感謝您的幫助..

回答

0

有多種方式做到這一點。 1.可以具有另一基站控制器用於此目的的,如下所示:

​​

然後其需要這些代碼的控制器,可以延長這個控制器。

  1. 對於此功能,您可以擁有[Attribute],並且可以使用此屬性修飾方法。

我會去2,這也是常見的代碼是什麼asp.net MVC使用如屬性,日誌,安全等