【问题标题】:How to pass Tweetinvi search result from controller to view如何将 Tweetinvi 搜索结果从控制器传递到视图
【发布时间】:2017-08-12 07:52:24
【问题描述】:

我正在使用 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; }         
}

提前致谢

【问题讨论】:

    标签: c# asp.net-mvc tweetinvi


    【解决方案1】:

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

    return View(model);
    

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

    例如:向模型类添加新属性 Tweets。

    模型变量可以从视图中访问——你可以这样声明它:

    @model SeatchTweetModel
    

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

    【讨论】:

    • 你能解释一下吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2020-07-20
    • 1970-01-01
    • 2016-05-12
    • 2013-12-30
    • 2015-10-15
    • 1970-01-01
    相关资源
    最近更新 更多