Jeroko
Jeroko

Reputation: 313

Using modelbinding in Nancy just does not work. Keep getting empty strings

EDIT: So i fixed this by turning TweetText into a property. The question that remains is why? what changed so that it just started working?

This is a portion of the app I'm writing:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nancy;
using Nancy.Hosting.Self;
using Nancy.ModelBinding;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var host = new NancyHost(new Uri("http://localhost:8080")))
            {
                host.Start();
                Console.ReadLine();
            }
        }
    }

    public class MyModule : NancyModule
    {
        public static List<Tweet> Tweets = new List<Tweet>();

        public MyModule()
        {
            Get["/add"] = param =>
            {
                return View["add.html"];
            };

            Post["/add"] = param =>
            {
                var model = this.Bind<TweetModel>();
                Tweets.Add(new Tweet(model.TweetText));
                return this.Response.AsRedirect("/");
            };
        }
    }

    public class Tweet
    {
        public int id;
        public string TweetText;
        public DateTime date;

        public Tweet(string tweet)
        {
            this.TweetText = tweet;
            this.date = DateTime.Now;
        }
    }

    [Serializable]
    public class TweetModel {
        public string TweetText;
    }
}

and here is add.html:

<html>
<head>
<title>
    Title
</title>
</head>
<body>
<form method="post" action="/add">
<input type = "text" name = "TweetText" />
<input type = "submit" value = "submit" />
</form>
</body>
</html>

The problem here is that no matter what i try, the model is not being populated with the text from the TweetText textbox, instead it just fills it with empty strings. I have even tried this.Request.Forms["TweetText"] to no avail. Almost all online resources I have read seem to imply that it "just works". This has not been the case for me. I would love to provide more info but I simply cannot figure out what is going wrong. The application does not log incoming requests and also does not provide any error messages. Any pointers on how to arrive at a solution would be appreciated!

Upvotes: 1

Views: 526

Answers (1)

Christian Horsdal
Christian Horsdal

Reputation: 4932

This is a known issue which is being fixed for v 1.0 of Nancy. But currently Nancy model binding only works on properties.

Upvotes: 2

Related Questions