2014-09-24 89 views
1

编辑:所以我通过将TweetText转成属性来解决此问题。仍然存在的问题是为什么?什么改变,以便它刚刚开始工作?在南希使用modelbinding不起作用。继续获得空串

这是应用程序,我正在写的一部分:

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

这里是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> 

这里的问题是,无论我怎么努力,该模型没有使用TweetText文本框中的文本填充,而是只填充空字符串。我甚至试过this.Request.Forms["TweetText"]无济于事。几乎所有我读过的在线资源似乎暗示它“只是有效”。对我而言,情况并非如此。我很乐意提供更多信息,但我根本无法弄清楚发生了什么问题。应用程序不会记录传入的请求,也不会提供任何错误消息。任何关于如何到达解决方案的指针,将不胜感激!

回答

2

这是一个已知问题,正在针对南希的1.0版进行修复。但是目前Nancy模型绑定仅适用于属性。