'详细信息'在 asp.net mvc 中,路由似乎没有被删除

发布于 2024-08-31 07:50:42 字数 1838 浏览 1 评论 0原文

我正在尝试从 http://localhost:1985/Materials/Details/2/Steel 删除 Details 但我的路线似乎不起作用......

编辑:

 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 = "" }
        );

将下面的答案放在我的路线集合中,我的索引页无法调用 jsonresult 控制器方法...

  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);
    }
   }

并且我的索引页有一个使用 json 结果的 jquery 函数...

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

这个 jquery函数似乎没有调用 jsonresult 控制器方法......但是如果我首先指定 Default 路由,它就会工作......

当通过 firebug 检查时,它会显示这个,

参数字典包含“CrMVC.Controllers.MaterialsController”中方法“System.Web.Mvc.ActionResult Details(Int32)”的不可空类型“System.Int32”的参数“id”的 null 条目。要使参数可选,其类型应该是引用类型或 Nullable 类型。
参数名称:parameters

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼趣 2024-09-07 07:50:42

你的路线是倒退的。它们按照添加到集合,以便首先匹配默认路由。


编辑

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=""} 
);

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=""} 
);
感性 2024-09-07 07:50:42

尝试使用routelink帮助器为此请求生成正确的URL,以便您可以检查它是否完全正确,然后在JavaScript中使用该URL。您的 URL 可能不太正确,从而导致了问题。

您可以使用routelink帮助器在链接上放置一个URL,只是为了获取它并查看它应该是什么样子 - 然后将其与AJAX请求中的实际URL进行比较。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文