Mir Gulam Sarwar
Mir Gulam Sarwar

Reputation: 2648

Passing Two Optional parameter to RedirectToRoute

I am passing 2 nullable parameters to a Products action. However I am forced to pass some value in mallId, otherwise I get a no route table matches found error. I want to pass null in mallId and receive in the Products action.

return RedirectToRoute("Products",
                      new
                      {   
                          mallId =(Int32?)null,
                          storeId =(Int32?)storeProducts.StoreId
                      });


[Route("Mall/{mallId?}/Store/{storeId?}/Products", Name = "Products")]
public ActionResult Products(string mallId, long? storeId)
{
    return View(products);
}

Attribute routing is cracking my head but it is great too.

Upvotes: 1

Views: 889

Answers (2)

Fals
Fals

Reputation: 6839

You should provide a default value for mallId. The way you distribute the parameters makes impossible to not provide the mallId in the route composition:

[Route("Mall/{mallId=all}/Store/{storeId?}/Products", Name = "Products")]
public ActionResult Products(string mallId = "all", long? storeId = null)
{
    if(mallId == "all")
       //do something

    return View(products);
}

Upvotes: 2

Dom
Dom

Reputation: 716

[Route("Mall/{mallId?}/Store/{storeId?}/Products", Name = "Products")]
public ActionResult Products(string mallId = null, long? storeId)
{
    return View(products);
}

and don't pass in a value for mallId

Upvotes: 2

Related Questions