简体   繁体   English

ViewDataFactory和强类型母版页

[英]ViewDataFactory and strongly typed master pages

Im trying to get my strongly typed master page to work in my ASP MVC 2.0 application. 我试图让我的强类型母版页在我的ASP MVC 2.0应用程序中工作。 I have come far with the help of these two posts: 在这两篇文章的帮助下,我走得很远:

Passing data to Master Page in ASP.NET MVC 将数据传递到ASP.NET MVC中的母版页

Strongly Typed ASP.Net MVC Master Pages 强类型的ASP.Net MVC主页

Problem is that im not sure how to get that ViewDataFactory code to work, this is my code: 问题是我不确定如何使ViewDataFactory代码正常工作,这是我的代码:

BaseController.cs BaseController.cs

public class BaseController : Controller
{        
    private IPageRepository _repPage;

    public BaseController(IPageRepository repPage)
    {
        _repPage = repPage;
    }

    protected T CreateViewData<T>() where T : MasterViewData, new()
    {
        IViewDataFactory factory = new ViewDataFactory();

        IEnumerable<Page> pages = _repPage.GetAllPages();

        return factory.Create<T>(pages);
    }
}

HomeController.cs HomeController.cs

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        HomeViewData viewData = CreateViewData<HomeViewData>();

        viewData.Name = "Test";

        return View("Index", viewData);
    }

    public ActionResult About()
    {
        return View();
    }
}

ViewDataFactory.cs ViewDataFactory.cs

public interface IViewDataFactory
{
    T Create<T>(IEnumerable<Page> pages) where T : MasterViewData, new()
}

public class ViewDataFactory : IViewDataFactory
{
    public ViewDataFactory()
    {
    }  
}

HomeViewData.cs HomeViewData.cs

public class HomeViewData : MasterViewData
{
    public string Name { get; set; }
}

MasterViewData MasterViewData

public class MasterViewData
{
    public IEnumerable<Page> Pages { get; set; }
}

When I build the solution I get the follwing build error: 构建解决方案时,出现以下构建错误:

"; expected" in ViewDataFactory.cs

Which points to the code snippet: 指向代码段:

T Create<T>(IEnumerable<Page> pages) where T : MasterViewData, new()

I guess im missing something essential, im new to this and any help would be appreciated! 我想我错过了一些必不可少的东西,对此我是新来的,任何帮助将不胜感激!

Why don't you just add a semicolon at the end of that line? 您为什么不只在该行的末尾添加分号? :) :)

Abstract methods (as well as interface methods) have a semicolon in place of a body. 抽象方法(以及接口方法)以分号代替主体。

I finally got it working thanks to Aviad P. 's suggestions and some trial and error. 由于Aviad P.的建议以及一些反复试验,我终于使它工作了。

This is how my IViewDataFactory ended up looking like: 这就是我的IViewDataFactory最终的样子:

public interface IViewDataFactory
{
    T Create<T>(IEnumerable<Page> pages) where T : MasterViewData, new();
}

public class ViewDataFactory : IViewDataFactory
{        
    public T Create<T>(IEnumerable<Page> pages) where T : MasterViewData, new()
    {
        T t = new T();
        t.Pages = pages;
        return t;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM