0

I've got a simple action method that lists some data from a repository. The view for this action method uses simple paging. The URL can be clicked on, or entered as server:port/product/2 where 2 is the page number. If the user enters in a page number that's greater than the number of pages of data, I want to redirect the user to server:port/product/1 first and the notify them they were redirected. The redirecting part works, but I can't seem to find a way to get the value passed to the action method.

EDIT: I used QueryString incorrectly in this question, what I really want is the parameter passed to the action method.

ProductController

 public ActionResult List(int page =1)
        {
            ProductsListViewModel model = new ProductsListViewModel();
            model.Products = _repository.Products.OrderBy(x => x.ProductId)
                                        .Skip((page -1) * 4)
                                        .Take(4);
            model.PagingInfo = new PagingInfo()
            {
                CurrentPage = page,
                ItemsPerPage = 4,
                TotalItems = _repository.Products.Count()
            };
            //this correctly redirects
            if (page > model.PagingInfo.TotalPages)
            {
                return RedirectToAction("List", new { page = 1 });
            }
            return View(model);
        }

JavaScript

   var pageParam = "@Request.QueryString["id"]"
    var pageTotal = "@Model.PagingInfo.TotalPages";
    var    pageCurrent ="@Model.PagingInfo.CurrentPage";
    console.log('this is the current page: ' + pageCurrent);
    console.log(pageTotal);
    console.log(pageParam);

    function notifyRedirect(pageTotal, pageParam) {
        if (pageParam > pageTotal) {
            alert('you entered ' + pageParam + ', an invalid page parameter and were redirected');
            window.location = '/Product/List/1';
        }
    }
    notifyRedirect(pageTotal,pageParam);

Routes

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional }
            );

When the page loads, the pageTotal and pageCurrent variables are printed to the console, but I get an empty string when trying to get the QueryString value. Thinking that maybe I had the name of the parameter wrong, I decided to use the integral index of the QueryString and was presented with the error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

How is it that http://localhost:49997/product/list/2, the fully qualified URL still gives me an empty QueryString value? How can I use JS to notify the user of the redirect?

6
  • QueryString is part of a url that comes after "?" and consists of "&"-separated "name=value" pairs - you don't have it Commented Feb 5, 2014 at 18:38
  • @wootscootinboogie, Do you want to notify first and then redirect? or redirect first and then notify? Your javascript seems to be doing - Notify and redirect, but in your question, I see you alert and then redirect. Commented Feb 5, 2014 at 19:07
  • @ramiramilu I would like to redirect and then notify. I don't know what I'm missing, but even using Request["id"] returns me nothing. However, if I use Request.Url I can see the parameter as part of the URL, but I can't seem to get a hold of it for any JS use. Commented Feb 5, 2014 at 19:12
  • With MVC routing, Request does not carry id as a separate item (not in QueryString, not in Form, etc). id is part of url. It is MVC framework that extracts id's value from url based on routing and passes it as action parameter if parameter name matches that in the route. Commented Feb 5, 2014 at 19:15
  • @Igor That's good to know. Check the updated routes section, I removed all but one route and yet when I try to print the parameter my List view with @Request["id"] I get a blank string. Why would this be happening? Commented Feb 5, 2014 at 19:19

2 Answers 2

1

Perhaps I'm missing something, but it seems like your question is "How do I get a query string value from a URL without a query string."

Both your redirect URL, /Product/List/1 and your last URL mentioned, http://localhost:49997/product/list/2, have no query string, so any attempt to access anything in Request.QueryString is going to return null.

If you need something in the query string on the redirect then you need to add that to the redirect URL:

window.location = '/Product/List/1' + ((pageParam != '') ? '?id=' + pageParam : '');
Sign up to request clarification or add additional context in comments.

2 Comments

I've conflated the parameter passed to the action method and a query string as the same thing. What I'm really after is the parameter that was passed to the action method.
Well, that wouldn't be achieved with Request.QueryString. Try just Request['page'] (since as @Igor noted your action accepts "page", not "id").
1

Change the parameter name of the action method from page to id to agree with MapRoute. Then you don't need the first MapRoute, if you don't skip the name of the action in the url.

1 Comment

I had tried that as well. As noted in the edit, what I really want to do is be able to get the parameter that was passed to the action method, and not a query string per se.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.