views:

46

answers:

2

I am trying to remove Details from http://localhost:1985/Materials/Details/2/Steel but some how my route doesn't seem to work...

Edit:

 routes.MapRoute(
             "Materials", // <-- Above default
             "Materials/{id}/{name}",
             new { controller = "Materials", action = "Details", id = "", name = "" }
         );

        routes.MapRoute(
            "Default", // <-- Last route, kind of a "catch all"
            "{controller}/{action}/{id}/{name}",
            new { controller = "Materials", action = "Index", id = "", name = "" }
        );

placing the answer below in my route collection my index page failed to call to jsonresult controller method....

  public class MaterialsController : Controller
  {
   public ActionResult Index()
    {
        return View("Materials");
    }

    public JsonResult GetMaterials(int currentPage,int pageSize)
    {
        var materials = consRepository.FindAllMaterials().AsQueryable();
        var count = materials.Count();
        var results = new PagedList<MaterialsObj>(materials, currentPage-1, pageSize);
        var genericResult = new { Count = count, Results = results };
        return Json(genericResult);
    }
   }

and my index page has a jquery function which uses the json result....

<script type="text/javascript">
$(document).ready(function() {
  $.ajax({
   url: "Materials/GetMaterials",
    data: {'currentPage': (currentPage + 1) ,'pageSize':5},
    contentType: "application/json; charset=utf-8",

This jquery function doesn't seem to call the jsonresult controller method...... But if i specify Default route first it works...

When inspected through firebug it shows this,

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'CrMVC.Controllers.MaterialsController'. To make a parameter optional its type should be either a reference type or a Nullable type.<br>Parameter name: parameters

+1  A: 

Your routes are backwards. They are matched in the order that they are added to the collection so the Default route gets matched first.


EDIT:

routes.MapRoute(
    "Materials",
    "Materials/{id}/{name}",
    new { controller = "Materials", action = "Details", id = "", name = "" },
    new {id= @"\d+" } // Prevent routes in the form of {controller}/{action}/{id} from matching.
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}/{name}",                          
    new { controller = "Materials", action = "Index", id = "" ,name=""} 
);
R0MANARMY
@Romanarmy what should i change? Route1 or route2?
Pandiya Chendur
@Romanary ya just saw the links...
Pandiya Chendur
@Pandiya Chendur: Added an example of what I meant by "Your routes are backwards." HTH
R0MANARMY
@Romanarmy look at my edit....
Pandiya Chendur
@Pandiya Chendur: So the reason the index action failed is because now the first route is matches everything. Basically it has no way of telling the difference between `Materials/123/some-name` and `Materials/Index/123/some-name`. The article I linked to gets around this by specifying a different prefix for routes =(.
R0MANARMY
@Pandiya Chendur: You could try creating a route constraint like so to make sure ID only matches numbers (or whatever your IDs happen to be) http://www.asp.net/mvc/tutorials/creating-a-route-constraint-cs
R0MANARMY
+1  A: 

Try using the routelink helper to generate the correct URL for this request, so you can check that you have it completely correct, then use that URL in your JavaScript. It is possible that your URL isn't quite right, which is causing the problem.

You can use the routelink helper to put a URL on a link just to get it and see what it should look like - then compare that to your actual URL in the AJAX request.

Sohnee
@Sohnee any example of route link helper....
Pandiya Chendur
In your view, you type "Html.RouteLink(" and it will prompt you for the details. Hope this helps.
Sohnee
@Sohnee thanks for ur update... I ll definitely try this...
Pandiya Chendur