MVC 4自定义路由按ID搜索

MVC 4 Custom route to search by ID

我想在 Table 上按 ID 搜索以下路线 "ActionRoute"。 如果我放置下一条路线,则不会显示任何数据,只有空白 table 和空视图。 a要搜索的ID是1它是一个十进制值。

我能做什么错了,我应该纠正什么才能正常工作?

谢谢

http://localhost:62357/ActionApprovers/Action/1

Issue when search in Route

所以我写了一个在教程中找到的控制器,但它不起作用

public ActionResult Action(decimal? idprod)
{
        Entities db = new Entities();
        var s = from r in db.TB_RS_PROD
                select r;
        s = s.Where(b => b.IDPROD == idprod);
        return View(s);
}

这是我的观点

@model IEnumerable<MvcInventory.TB_RS_PROD>
@{

}

<html>
<table class="table table-striped">
    <tr>
        <td>ID PRODUCT</td>
        <td>PRODUCT NAME</td>
        <td>QUIANTITY</td>
        <td>DATE</td>
        <td>STATUS</td>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.IDPROD</td>
            <td>@item.PROD_NAME</td>
            <td>@item.QUANTITY</td>
            <td>@item.UPDATE_DATE</td>
            <td>@item.STATUS</td>
        </tr>
    }
</table>
</html>

这是 RouteConfig.cs

上的路线
        public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "ActionRoute",
            url: "ActionApprovers/{idprod}",
            defaults: new { controller = "ActionApprovers", action = "Action", idprod = 0 },
            constraints: new { beerid = @"\d+" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

    }

Global.asax

public class MvcApplication : System.Web.HttpApplication
{

    protected void Application_Start()
    {

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }
}

在很多网站上搜索后,我读到了有关 ID 路由请求的信息。

public ActionResult Action(decimal? idprod)
{
    Entities db = new Entities();
    idprod = Convert.ToDecimal(Request["idprod"]); // This is the solution
    var s = from r in db.TB_RS_PROD
            select r;
    TempData["rt"] = idprod;
    s = s.Where(b => b.IDPROD == idprod);
    return View(s);
}