Nancy Model Binding

Paddy1990 picture Paddy1990 · Mar 16, 2014 · Viewed 7.3k times · Source

Hi I'm learning Nancy and I'm trying to bind to a model, but I'm getting the error:

Error   8   'NancyFxTutorial.CarModule' does not contain a definition for 'Bind' and no extension method 'Bind' accepting a first argument of type 'NancyFxTutorial.CarModule' could be found (are you missing a using directive or an assembly reference?) C:\Development\Projects\C#\Web\Nancy\NancyFxTutorial\NancyFxTutorial\CarModule.cs

model:

public class BrowseCarQuery
{
    public string Make { get; set; }
    public string Model { get; set; }
}

public class CarModule : NancyModule
{
    public CarModule()
    {
        Get["/status"] = _ => "Hello World";

        Get["/Car/{id}"] = parameters =>
            {
                int id = parameters.id;
                return Negotiate.WithStatusCode(HttpStatusCode.OK).WithModel(id);
            };

        Get["/{make}/{model}"] = parameters =>
        {
            BrowseCarQuery model = new BrowseCarQuery();

            var carQuery = this.Bind<>()

        };
    }
}

any clues?

Thanks in advance

Answer

nemesv picture nemesv · Mar 16, 2014

The Nancy model binding methods are defined as extension methods on the NancyModule class.

And these extension methods can be found in the Nancy.ModelBinding namespace.

So you need to using the Nancy.ModelBinding namespace to access the Bind() and BindTo() methods.

So add this line to your source file:

using Nancy.ModelBinding;