0

Detailsから削除しようとしてhttp://localhost:1985/Materials/Details/2/Steelいますが、ルートがうまくいかないようです...

編集:

 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 で検査すると、これが表示されます。

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

4

2 に答える 2

1

routelink ヘルパーを使用して、このリクエストの正しい URL を生成してみてください。これにより、完全に正しいことを確認してから、その URL を JavaScript で使用できます。URL が正しくない可能性があり、それが問題の原因となっています。

routelink ヘルパーを使用して URL をリンクに配置し、それを取得してどのように表示されるかを確認し、それを AJAX リクエストの実際の URL と比較します。

于 2010-05-06T06:45:24.103 に答える
1

あなたのルートは逆です。コレクションに追加された順に照合されるため、デフォルトルートが最初に照合されます。


編集

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=""} 
);
于 2010-05-06T05:10:16.080 に答える