开发者

RenderView with MVC2

开发者 https://www.devze.com 2022-12-16 11:55 出处:网络
I am using MVC preview 2 framework to develop web sites and I am following MVCStorefront tutorials to get a good feel on MVC.

I am using MVC preview 2 framework to develop web sites and I am following MVCStorefront tutorials to get a good feel on MVC.

Can you tell me why I can't use RenderView() method ?

Am I missing something or can I use View() instead ? What's the difference between these methods..

Thanks

Here is where Rob is using RenderView in his tutorial.

[TestM开发者_StackOverflow中文版ethod]
    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);
    }

I can't use RenderView. It says " the name 'RenderView' does not exist in the current context

Here's the link : http://www.asp.net/learn/mvc-videos/video-357.aspx

Here is an index method from the CatalogController class :

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);
    }

I am also having a problem with the casting of result.ViewData in CatalogData type which is class that contains data. It says : Cannot convert type System.Web.Mvc.ViewDataDictionary to Commerce.MVC.Web.Controllers.CatalogController.CatalogData


The video you are watching is unfortunately outdated - it is from ASP.NET MVC 1.0 Preview 2. Since then ASP.NET MVC 1.0 RTM has shipped and there are previews of ASP.NET MVC 2 available.

In ASP.NET MVC 1.0 Preview 2 and earlier action methods returned 'void' so they had to explicitly perform a result, such as render a view:

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

In ASP.NET MVC 1.0 Preview 3 (Refresh?) and newer, action methods return a result object, which then actually performs the result:

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

The main reason this changed is that it allows for much better unit testing. Action methods now only perform the "application logic" and don't worry about exactly how to render a view. The unit test can simply inspect the results of the application logic and then verify that the next desired step was "render a view."

A lot of type names and method names have changed as well to make them shorter and simpler to use. For example, RenderView is just View and RenderViewResult is just RenderView.


My apologize if I resurrect a dead topic like this, but I was having the same issues as OP and I found my solution. Therefore I would reply here in case anybody who is following Rob's StoreFront series would find the solution also.

[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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号