2010-01-10 84 views
0

我正在使用MVC預覽2框架來開發網站,我正在關注MVCStorefront教程以獲得對MVC的良好感受。RenderView與MVC2

你能告訴我爲什麼我不能使用RenderView()方法嗎?

我錯過了什麼,或者我可以使用View()來代替? 什麼是這些方法之間的區別..

感謝

這裏就是羅布在他的教程中使用的RenderView。

[TestMethod] 
    public void CatalogController_IndexMethod_ShouldReturn_Categories_And_Data_For_Parent1() { 

     CatalogController c = new CatalogController(_repository); 

     RenderViewResult result = (RenderViewResult)c.Index("Parent1", "Sub10"); 

     CatalogController.CatalogData data = (CatalogController.CatalogData)result.ViewData; 

     Assert.IsNotNull(data.Category); 
     Assert.IsNotNull(data.SubCategory); 
     Assert.IsNotNull(data.SubCategory.Products); 
     Assert.IsTrue(data.SubCategory.Products.Count() > 0); 

     Assert.IsNotNull(result); 
    } 

我不能使用RenderView。它說:「名字‘的RenderView’在目前情況下

這裏不存在的鏈接: http://www.asp.net/learn/mvc-videos/video-357.aspx

這裏從CatalogController類的指數方法:

public ActionResult Index(string category, string subcategory) { 

     //instantiate the service 
     CatalogService svc = new CatalogService(_repository); 

     //the ViewData class 
     CatalogData data = new CatalogData(); 

     //pull all the categories for the navigation 
     data.Categories = svc.GetCategories(); 

     //pull the category based on subcategory name 
     data.Category = data.Categories.WithCategoryName(category); 

     //catch for bad data 
     if (data.Category == null) { 

      data.Category = data.Categories.DefaultCategory(); 

      data.SubCategory = data.Category.SubCategories[0]; 

     } else { 

      data.SubCategory = data.Categories.WithCategoryName(subcategory); 

      //catch for bad SubCategory 
      data.SubCategory= data.SubCategory ?? data.Category.SubCategories[0]; 

     } 
     return RenderView("Index",data); 
    } 

我也有在類型爲包含數據的CatalogData類型中的結果.ViewData的問題。它表示:無法將類型System.Web.Mvc.ViewDataDictionary轉換爲Commerce.MVC.Web.Controllers.CatalogController.CatalogData

+0

你爲什麼要使用預覽版2?釋放候選人現在不在。也可能是最好的解釋你想使用這種方法的情況。 – LiamB 2010-01-10 19:51:39

+0

聽起來,他在談論ASP.NET MVC 1.0預覽版2(這是1歲以上),而不是ASP.NET MVC 2預覽版2(這只是幾個月前)。 – Eilon 2010-01-10 20:02:51

+0

伯納德,你指的是哪一個Rob的教程?請提供一個鏈接。聽起來好像真的很老。此外,您正在顯示單元測試代碼,而不是控制器代碼。我沒有看到「RenderView」在那裏被使用 - 只是一個RenderViewResult(它不再存在 - 它現在被稱爲ViewResult)。 – Eilon 2010-01-10 20:05:10

回答

2

您正在觀看的視頻不幸已過時 - 它來自ASP.NET MVC 1.0 Preview 2.自此,ASP.NET MVC 1.0 RTM已發佈,並且有ASP.NET MVC 2的預覽可用。

public void Index() { 
    // do some work... 
    RenderView("Index"); 
} 

在ASP.NET MVC 1.0預覽3:

在ASP.NET MVC 1.0預覽2及早期操作方法,這樣他們必須明確地執行結果,如呈現視圖返回「無效」 (?刷新)和更新,操作方法返回一個結果對象,然後實際執行結果:

public ActionResult Index() { 
    // do some work... 
    return View("Index"); 
    // or you could also just say "return View();" and MVC figures out the view name 
} 

這個變化的主要原因是,它允許更好的單元測試。操作方法現在只執行「應用程序邏輯」,不用擔心如何來渲染視圖。單元測試可以簡單地檢查應用程序邏輯的結果,然後驗證下一個需要的步驟是「渲染視圖」。

許多類型名稱和方法名稱也發生了變化,以使它們更短且更易於使用。例如,RenderView只是View和RenderViewResult就是RenderView。

0

我的道歉,如果我復活這樣的死話題,但我有OP相同的問題,我發現我的解決方案。因此,如果任何跟隨Rob的StoreFront系列的人也能找到解決方案,我會在此回覆。

[TestMethod] 
public void CatalogController_IndexMethod_ShouldReturn_Categories_And_Data_For_Parent1() { 

    CatalogController c = new CatalogController(_repository); 

    ViewResult result = c.Index("Parent1", "Sub10") as ViewResult; 

    CatalogController.CatalogData data = result.ViewData.Model as CatalogController.CatalogData; 

    Assert.IsNotNull(data.Category); 
    Assert.IsNotNull(data.SubCategory); 
    Assert.IsNotNull(data.SubCategory.Products); 
    Assert.IsTrue(data.SubCategory.Products.Count() > 0); 
    Assert.AreEqual("Parent1", data.Category.Name); 
    Assert.AreEqual("Sub10", data.SubCategory.Name); 

    Assert.IsNotNull(result); 
}