简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM