Reputation: 2648
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
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
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