gpt4 book ai didi

c# - ViewDataFactory 和强类型母版页

转载 作者:行者123 更新时间:2023-11-30 20:13:01 25 4
gpt4 key购买 nike

我试图让我的强类型母版页在我的 ASP MVC 2.0 应用程序中工作。在这两个帖子的帮助下,我已经走了很远:

Passing data to Master Page in ASP.NET MVC

Strongly Typed ASP.Net MVC Master Pages

问题是我不确定如何让 ViewDataFactory 代码工作,这是我的代码:

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

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

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

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

HomeViewData.cs

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

主视图数据

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

当我构建解决方案时,出现以下构建错误:

"; expected" in ViewDataFactory.cs

指向代码片段:

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

我想我遗漏了一些重要的东西,我是新手,我们将不胜感激!

最佳答案

为什么不在该行的末尾添加一个分号? :)

抽象方法(以及接口(interface)方法)用分号代替正文。

关于c# - ViewDataFactory 和强类型母版页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1989927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com