views:

400

answers:

3

Im trying to get my strongly typed master page to work in my ASP MVC 2.0 application. I have come far with the help of these two posts:

Passing data to Master Page in ASP.NET MVC

Strongly Typed ASP.Net MVC Master Pages

Problem is that im not sure how to get that ViewDataFactory code to work, this is my code:

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

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!

+5  A: 

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.

Aviad P.
Wish it was that easy, when I do that I get another build error:'Website.Models.ViewDataFactory' does not implement interface member 'Website.Models.IViewDataFactory.Create<T>(System.Collections.Generic.IEnumerable<Website.Models.Page>)'
Martin
That's because it really doesn't, I assume you're using Visual Studio, if that's the case, right click on the `IViewDataFactory` in the `ViewDataFactory` class declaration, and select `Implement Interface`
Aviad P.
Thank you, it solved my problem so far. But what shall I put inside of the implemented interface. The solution builds as it should but I get an error while running it. `The method or operation is not implemented.` That is because I still have the `throw new NotImplementedException();` inside of it, but what shall I put inside?
Martin
Well, I know virtually nothing about MVC, the syntax error caught my attention, but I can't help you with the actual issue.
Aviad P.
Well thank you so far, you solved my problem. I will keep it open a while more to maybe get some more help and then I will tag it as answered.
Martin
+1  A: 

I finally got it working thanks to Aviad P.'s suggestions and some trial and error.

This is how my IViewDataFactory ended up looking like:

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;
    }
}
Martin
A: 

(Thought I should post this as a response under an existing question but do not see any link for that...)

Anyways, what is IPageRepository?

Jason