Question

I'm trying to display some information from a model to my view in Nancy.

Viewmodel

public class Fixtures
{
    public int Id { get; set; }
    public Users HomeUser { get; set; }
    public Users AwayUser { get; set; }
}

Module

Get["/fixtures"] = _ =>
            {
                var model = new List<Fixtures>();

                model.Add(new Fixtures() { Id = 1, HomeUser = new Users() { id = 1, Name = "Paddy" }, AwayUser = new Users() { id = 2, Name = "Dave" } });
                model.Add(new Fixtures() { Id = 2, HomeUser = new Users() { id = 3, Name = "Scott" }, AwayUser = new Users() { id = 4, Name = "Chris" } });

                return View["Fixtures", model];
            };

View

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<System.Collections.Generic.List<Fifa.Leaderboard.Data.ViewModel.Fixtures>>

@{

    Layout = "_Layout.cshtml";

}

<div id="fixtures">

    @foreach (var fixture in Model)

    {'

        <p>@fixture.HomeUser</p>

        <p>VS</p>

        <p>@fixture.AwayUser</p>

    }

</div>

Anyone know why I'm getting this error?

Was it helpful?

Solution

Problem was found as mentioned in comments of original question.

Nancy does not have a concept of a Shared Views folder like ASP.NET MVC.

The normal way to do this would be to put the _Layout.cshtml in the root of the /Views folder.

If you want to avoid the view being in the root folder or avoid specifying the full path, you can add a View Location to the list like so:

public class CustomConventionsBootstrapper : DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
    {
        this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
        {
            return string.Concat("views/shared/", viewName);
        });
    }
}

Documentation can be found here:

https://github.com/NancyFx/Nancy/wiki/View-location-conventions#defining-custom-conventions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top