Patrik Gustafsson
Patrik Gustafsson

Reputation: 198

How to get the value from a button click and use it in the same view MVC 5

I have a table with rss feeds and a button after each feed. When the user clicks the button I want to read the feed on the same page. I have everything working, except one thing. How do I send the url of the newsfeed back to the controller and then use it to show the news feeds, when the user clicks the button.

Here I show the urls of the newsfeeds in a table

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

Here i want to display the newsfeeds

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

Here is the line of code I tried above to accomplish my goal.

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

What should I write instead? Here is my controller:

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

Edit: Solution

I couldn't get the Ajax to work. I will have to look into Ajax more :) But I finally found a solution that works.

In my controller I created a method GettheFeed like this:

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

In my indexview I added this line of code

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

Instead of

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

And I finally got it to work after making sure that I checked if a variable is null instead of checking if a ViewBag is null in the code that displays the newsfeed.

Upvotes: 0

Views: 1296

Answers (3)

Kypaz
Kypaz

Reputation: 411

You need to use Ajax

For example, fire this ajax when the user click on the button :

$.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);
    }
});

and with an getRssFeed function in your index controller :

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

The alerted data will be the data you need to display. ( for example affect the data to your 'inlast' variable.

EDIT : Your buttons in the loop will look like this, (based on ur code)

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

And your js :

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

                                          }
                                  });
   });

And put the getRssFeed function above in your controler. I think what I wrote works, but it's ugly because your return your view (all the html, js ... etc) though you only need the modification of your model.

So I advise you to use partial views

Upvotes: 2

Fanjo Lama
Fanjo Lama

Reputation: 581

You controller will be

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

Upvotes: 0

CrazyDev
CrazyDev

Reputation: 157

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

Upvotes: 0

Related Questions