'Details' in asp.net mvc routing doesn't seem to get removed
I am trying to remove Details
from http://localhost:1985/Materials/Details/2/S开发者_StackOverflowteel
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
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=""}
);
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.
精彩评论