Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:27:23+00:00 2026-06-10T15:27:23+00:00

Here is the view code: @helper sortLink(string name, int id) { <a href=@Url.Action(Products, Home,

  • 0

Here is the view code:

@helper sortLink(string name, int id)
{
<a href="@Url.Action("Products", "Home", new { sortBy = id, isasc = (id ==     ViewBag.sortBy ? !@ViewBag.isAsc : @ViewBag.isAsc).ToString().ToLower() })">@name</a> 
}

<h2 class="center">Products</h2>

<table class="Products">
@* header *@

<tr>
<th>@sortLink("ID",1)</th>
<th>@sortLink("Name",2)</th>
<th>@sortLink("Number", 3)</th>
<th>@sortLink("Color", 4)</th>
<th>@sortLink("Standard Cost", 5)</th>
<th>@sortLink("List Price", 6)</th>
<th>@sortLink("Size", 7)</th>
<th>@sortLink("Weight", 8)</th>
</tr>

This is the controller code :

  {
    AdventureWorksLT2008R2Entities db = new AdventureWorksLT2008R2Entities();
    const int pageSize = 10;

    [HttpGet]
    public ActionResult Products(int page = 1, int sortBy = 1, bool isAsc = true)
    {
        IEnumerable<Product> products;

        #region sorting
        switch (sortBy)
        {
            case 1:
                products = isAsc ? db.Products.OrderBy(p => p.ProductID) : db.Products.OrderByDescending(p => p.ProductID);
                break;

            case 2:
                products = isAsc ? db.Products.OrderBy(p => p.Name) : db.Products.OrderByDescending(p => p.Name);
                break;

            case 3:
                products = isAsc ? db.Products.OrderBy(p => p.ProductNumber) : db.Products.OrderByDescending(p => p.ProductNumber);
                break;

            case 4:
                products = isAsc ? db.Products.OrderBy(p => p.Color) : db.Products.OrderByDescending(p => p.Color);
                break;

            case 5:
                products = isAsc ? db.Products.OrderBy(p => p.StandardCost) : db.Products.OrderByDescending(p => p.StandardCost);
                break;

            case 6:
                products = isAsc ? db.Products.OrderBy(p => p.ListPrice) : db.Products.OrderByDescending(p => p.ListPrice);
                break;

            case 7:
                products = isAsc ? db.Products.OrderBy(p => p.Size) : db.Products.OrderByDescending(p => p.Size);
                break;

            case 8:
                products = isAsc ? db.Products.OrderBy(p => p.Weight) : db.Products.OrderByDescending(p => p.Weight);
                break;
        }
        #endregion

        products = db.Products
            .OrderBy(p=>p.ProductID)
            .Skip((page - 1) * pageSize)
            .Take(pageSize)
            .ToList();

        ViewBag.CurrentPage = page;
        ViewBag.PageSize = pageSize;
        ViewBag.TotalPages = Math.Ceiling((double)db.Products.Count()/pageSize);

        return View(products);

I am following a tutorial and struck on the sorting part where it keeps showing this error,
i need your help to get remove the error and please explain whats going on the Sortlink helper method, i wan’t able to understand it well. The person who wrote this tutorial didn’t explain much on this part.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:27:25+00:00Added an answer on June 10, 2026 at 3:27 pm

    I’m guessing the problem is that you’re trying to use ViewBag.isAsc in the view, but never assigned it. Also, after the case statement, you’re resetting the products variable, which completely invalidates the ordering you set up in the case statement– remove that. So after it should look like this:

    switch(sortBy) {
        // include all the case statements here as you have them
    
        // you need to add a default section to make it a valid switch statement
        default:
        break;
    }
    #endregion
    
    // (remove the products = statement that was here, it's not needed
    
    ViewBag.CurrentPage = page;
    ViewBag.PageSize = pageSize;
    ViewBag.TotalPages = Math.Ceiling((double)db.Products.Count()/pageSize);
    
    // add these two lines, as required by the View
    ViewBag.isAsc = isAsc;
    ViewBag.sortBy = sortBy;
    
    
    return View(products
        .Skip((page - 1) * pageSize)
        .Take(pageSize)
        .ToList()
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading some RAW code from here http://www.zfsnippets.com/snippets/view/id/17/output/raw/table-view-helper where the author is doing a
i wanna do a single selection in my table view here is my code
you can view full source code here dpaste.com/hold/167199 Error: delete() takes exactly 2 arguments
here the following code is used to view the present modal view controller. [[self
Here is ALL my code for this specific view controller. As the title of
here is my code in Controller var q = context.post; return View(q); in view
Here's the code I'm using to make a custom AlertDialog. public void onButtonClicked(View v)
The is my code.I am here pupulating a list view with some array data.when
I am trying to get the code found here: http://snipplr.com/view/26643/mbprogresshud-with-an-asynchronous-nsurlconnection-call/ to work in my
[QueryInterceptor(Somethings)] public Expression<Func<Something, bool>> OnSomethings() { // Code here } I had a view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.