Asp.net MVC action and view issue

I am stuck in a weird situation. I have a controller action which is passed a category string. Then the action method matches the string in the database and collects the sub categories and products for it. Now, What I want is to achieve pagination for the products if they are greater than 10. The view is a strongly-typed one for Category Model

Following is the code for action method.

    public ActionResult Catalog(string id)
    {
        Category catalog = pe.Categories.Where(cat => cat.CategoryName == id).Single();
        return View(catalog);
    }

I have done pagination in my other project like following but in that case the collection was an IQueryable which can't be in the above case since its just for one category. Also, I am already passing an argument in the above function so how can i pass two.

    public ViewResult Index(int? page)
    {
        IQueryable<Album> albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist).OrderBy(a => a.Title);
        int pageIndex = page ?? 1;
        return View(albums.ToPagedList(pageIndex, PageSize));
    }

Please tell me how will you tackle such scenario.

Answers


Use a viewmodel to collect just the information that you want to show in your view. In your selection logic, select into the viewmodel the top-level attributes from the category and the relevant parts of the association. You can certainly have more than one parameter to the method. Supply the extra parameters to the route values in your ActionLink or Form helper methods and it should construct the Url properly to reference your action. Note you will need to change the type of the model in the view as well to match that from the action.

public ActionResult Catalog(string id, int? page)
{
    page = page ?? 1;
    var category = pe.Categories.SingleOrDefault(cat => cat.CategoryName == id);
    var model = new CatalogViewModel
                {
                       ID = category.ID,
                       Name = category.CategoryName,
                       Subcategories = category.Subcategories,
                       Products = category.Products.ToPagedList( page, PageSize )
                };

    return View(model);
}

View (sample)

@if (Model.Products.Page < Model.Products.Pages)
{
    @Html.ActionLink( "Next",
                      "catalog",
                      new { id = Model.ID, page = Model.Products.Page + 1 } ) 
}

You have to generate your paged results before sending it to the view. I guess you have to take a page index parameter too.

public ActionResult Catalog(string id, int? pageIndex)
{
    Category catalog = pe.Categories.Where(cat => cat.CategoryName == id).Single();
    int pageIndex = page ?? 1;
    return View(catalog.Products.ToPagedList(pageIndex, PageSize));
}

You better use IEnumerable interface instead of IQueryable when you are doing operations in memory. Make your view strongly typed to IEnumerable<Product> and it should work.

If you are going to use any data from Category class in your view, you better create a ViewModel and put that data and your page result inside it.


Need Your Help

Best design for windows forms sharing a database?

.net sql winforms network-programming

I plan to develop a windows forms application (or possibly WPF), which may be installed/distributed on many computers all on one network.

Using method convertTo in opencv java wrapper

java android opencv wrapper javacv

any body please help me to understand how to use method convertTo in JavaCV. i was trying and searching a lot of time and not find the proper answer. so my problem is, when i try to convert type of...