Shihan Khan
Shihan Khan

Reputation: 2188

Sorting without reloading page in asp.net mvc

I'm new to asp.net & I'm trying to making a website where user can sort a table after login. So far sorting is working fine but everytime I click on the link, whol page reloads & data gets sorted. Instead I want only the table gets updated after clicking the link. I'm trying to use AJAX in my view but nothing happened. Here are my codes,

Controller

public ActionResult Login(string sortOrder)
    {
        if (Session["UserNAME"]!=null)
        {
            ViewBag.CodeSort = String.IsNullOrEmpty(sortOrder) ? "code_desc" : "";
            var sortedOut = new MkistatVsUserLogin { mkistats = dsedb.mkistats.AsQueryable() };

            switch (sortOrder)
            {
                case "code_desc":
                    sortedOut.mkistats = sortedOut.mkistats.OrderByDescending(s => s.MKISTAT_CODE);
                    break;
                default:
                    sortedOut.mkistats = sortedOut.mkistats.OrderBy(s => s.MKISTAT_INSTRUMENT_CODE);
                    break;
            }
            return View(sortedOut);
        }
        else
        {
            return RedirectToAction("Home");
        }
    }

View

<th>@Html.ActionLink("Codename", "Login", new { sortOrder = ViewBag.CodeSort }, new AjaxOptions
        {
            HttpMethod = "GET",
            UpdateTargetId = "mktTable",
            InsertionMode = InsertionMode.Replace
        })</th>

How can I solve this problem? Really need this help badly. Tnx.

Upvotes: 0

Views: 940

Answers (1)

BrunoLM
BrunoLM

Reputation: 100321

Instead of using @Html you should be using @Ajax.

To enable Ajax in your application you need to have jQuery on your view. If you are creating the default setup it should be already included and setup.

Upvotes: 2

Related Questions