user2158164
user2158164

Reputation: 49

ASP.NET MVC results are not being filtered out

I have a page index.cshtml and in that i call another page test.cshtml using iframes. The page test.cshtml contains code to filter out some stuff. Even though the values are being filtered out when it comes to UI it is not showing the filtered values. Can you please help me in identifying what i am missing.

Below is the sample code

index.cshtml

<div class="wrapper">
        <div class="h_iframe">
            <img class="ratio" src="/images/ratio16x9.png" />
                <iframe src="@Url.Action("test","Home")" frameborder="0"></iframe>
        </div>
    </div>

test.cshtml

<div class="container">
    <div id="onpageloadvalues">

 @foreach (var catgoryabstractslists in ViewBag.abstracts)
        {
            <div><a href="#" data-id='@catgoryabstractslists.AbstractID' id='CategorySelect_@(catgoryabstractslists.AbstractID)' class="select-abstracts">Abstract #@catgoryabstractslists.AbstractID - @catgoryabstractslists.AbstractTitle</a></div>
           }

</div>

</div>

$('.select-abstracts').click(function () {
        var data = $(this).attr('data-id');
        $.ajax({
            url: '@Url.Action("GetAllAbstracts", "Home")',
            data: { abstractid: data },
            success: function (result) {
            },
            error: function () {
                alert('Error');
            }
        });
    });

Controller

public ActionResult GetAllAbstracts(string categoryname)
        {
            List<Abstracts> abstracts = Helpers.SchedOrgHelpers.LoadAbstracts();            
            abstracts = abstracts.Where(x => x.Category == categoryname).ToList();

            //Populate the Viewbag using the helpers method            
            ViewBag.abstracts = abstracts;

            return View("....");
        }

Upvotes: 2

Views: 52

Answers (1)

Shyju
Shyju

Reputation: 218732

You are making the call to server via ajax and inside that you are setting the filtered data to ViewBag, but you are not reading that again from server (your page is not executed at server again becacuse your call is via ajax).

What you should be doing is, instead of setting it to ViewBag, you should either send it as JSON data (and in your ajax success method, parse the json array and build the html markup and append/replace the specific DOM element) or a return partial view which uses this data to render whatever markup you need and replace the existing DOM element you want.

Upvotes: 2

Related Questions