如何从单击按钮获取值并在同一视图MVC 5中使用它

时间:2016-04-07 09:36:56

标签: c# asp.net-mvc

我有一个带有RSS提要的表格和每个提要后的按钮。当用户点击按钮时,我想在同一页面上阅读Feed。我有一切工作,除了一件事。当用户点击按钮时,如何将新闻源的网址发送回控制器,然后用它来显示新闻源。

在这里,我在表格中显示新闻源的网址

<table class="table">
    @{if (ViewBag.Rsslist != null)
    {
        foreach (var item in ViewBag.Rsslist)
        {
            <tr class="something">
                <td class="col-md-2">
                    @item.sTidning
                </td>
                <td class="col-md-2">
                    @item.SUrl
                    @{string rssurl = item.SUrl; }
                </td>
                <td class="col-md-2">
                    <a href="@Url.Action("Index", "Home",new { rssurl = ViewBag.Feed})" class="btn btn-success btn-sm">Open</a>
                </td>
            </tr>
        }
    }
    else
    {
        <tr class="something">
            <td class="col-md-2">No data to dsiplay</td>
        </tr>
    }}
</table>

这里我想显示新闻源

<div class="col-md-4">
    <table class="table">
        <tr>
            <td>
                @{var inlast = reader.Las(ViewBag.Feed); }
                @inlast.Title.Text
                <br />
                <a href="@inlast.Links[0].Uri">@inlast.Links[0].Uri</a><br />

                @{foreach (var item in inlast.Items)
                {
                    <a href="@item.Links[0].Uri">@item.Title.Text</a>
                    <br />
                }}
       </td>
    </tr>
</table>

以上是我为实现目标而尝试的代码行。

<a href="@Url.Action("Index", "Home",new { rssurl = ViewBag.Feed})" class="btn btn-success btn-sm">Open</a>

我应该写什么呢?这是我的控制器:

public class HomeController : Controller
{
    private Rss_DevEntities _db = new Rss_DevEntities();
    public ActionResult Index()
     {




            List<RSS_Head> rss_head = new List<RSS_Head>();
            rss_head = _db.RSS_Head.ToList();

            ViewBag.Rsslist = rss_head;
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }



    public ActionResult Flasare()
    { 
        return View();
    }
}
}

编辑:解决方案

我无法让Ajax工作。我将不得不更多地研究Ajax :)但我终于找到了一个有效的解决方案。

在我的控制器中,我创建了一个方法GettheFeed,如下所示:

public ActionResult GettheFeed(string rssfeed)
    {
        List<RSS_Head> rss_head = new List<RSS_Head>();
        rss_head = _db.RSS_Head.ToList();

        ViewBag.Rsslist = rss_head;

        ViewBag.Feed = rssfeed;
        return View("~/Views/Home/Index.cshtml");
    }
}

在我的indexview中,我添加了这行代码

@Html.ActionLink("Open", "GettheFeed", new { rssfeed = rssurl })

而不是

<a href="@Url.Action("Index", "Home",new { rssurl = ViewBag.Feed})" class="btn btn-success btn-sm">Open</a>

我确保检查变量是否为null而不是检查显示新闻源的代码中的ViewBag是否为空后,我终于开始工作了。

3 个答案:

答案 0 :(得分:2)

您需要使用Ajax

例如,当用户点击按钮时触发此ajax:

$.ajax({
    url: '@Url.Action("getRssFeed", "Home")',
    data: {rssurl = feedInfo}, //store the feed info in an attribute of your button for example
   type: 'POST',
   success: function(data) {
    alert(data);
    }
});

并在索引控制器中使用getRssFeed函数:

[HttpPost]
public ActionResult getRssFeed(string rssurl)
{
    ViewBag.Feed = rssurl ;
    return View();
}

警报数据将是您需要显示的数据。 (例如,将数据影响到您的&#39; inlast&#39;变量。

编辑: 循环中的按钮看起来像这样(基于你的代码)

<a class="loadRss" id="@item.SUrl" class="btn btn-success btn-sm">Open</a>

你的js:

  $(".loadRss").click(function() {
                                  $.ajax({
                                     url: '@Url.Action("getRssFeed", "Home")',
                                     data: {rssurl : $(this).attr('id')}, 
                                     type: 'POST',
                                     success: function(data) {

                                          }
                                  });
   });

并将getRssFeed函数放在你的控制器中。我认为我写的是有效的,但它很难看,因为你只需要修改你的模型即可返回你的视图(所有的html,js ......等)。

所以我建议你使用partial views

答案 1 :(得分:0)

Yes i agree with @kypaz , Use ajax which works fine without postback. You will get value of button.

答案 2 :(得分:0)

你的控制器将是

 public ActionResult Index(string rssurl)
 {
     List<RSS_Head> rss_head = new List<RSS_Head>();
     rss_head = _db.RSS_Head.ToList();

     ViewBag.Rsslist = rss_head;
     return View();
 }