如何从控制器传递Tweetinvi搜索结果查看

时间:2017-03-20 20:50:18

标签: c# asp.net-mvc tweetinvi

我正在使用Tweetinvi一个.NET C#库来访问Twitter REST API。

这是我的控制器

[HttpPost]
public ActionResult Index(SeatchTweetModel search)
{
    if (ModelState.IsValid)
    {
        Auth.SetUserCredentials("MyCONSUMER_KEY", "MyCONSUMER_SECRET", "MyACCESS_TOKEN", "MyACCESS_TOKEN_SECRET");

        var searchText = search.TextToSearch;
        var tweets = Search.SearchTweets(searchText );

        ModelState.Clear();
    }
    return View();
}

我尝试ViewBag但发生了NullReferenceException。

呈现推文搜索结果的最佳方式是什么,

这是我的模特,

public class SeatchTweetModel
{
    [Required(ErrorMessage ="This field is required")]
    [DisplayName("Search Tweet")]
    [StringLength(160, ErrorMessage =("Max 160 character allowed"))]
    public string TextToSearch { get; set; }         
}

提前致谢

1 个答案:

答案 0 :(得分:0)

将数据从控制器传递到视图的最佳方法是将它们存储在模型中。

return View(model);

Yo可以在模型中添加另一个属性并在那里存储结果。

例如:添加新属性推文到您的模型类。

可以从视图访问模型变量 - 您可以这样声明:

@model SeatchTweetModel

在视图中,您可以通过以下方式访问您的数据:@ Model.Tweets。

相关问题