【问题标题】:passing multiple parameter in mvc3在mvc3中传递多个参数
【发布时间】:2012-05-20 01:54:08
【问题描述】:

我有一个需要 3 个参数的控制器

public ActionResult Directives(string MachineName, int ProjectId, int CompanyId)

现在 URL 看起来像这样。

/Project/Directives?projectId=41&MachineName=Apple%20Macbook%20Luft

但是我如何让它看起来像这样。

/Project/Directives/41/AppleMacbookAir/

【问题讨论】:

标签: asp.net-mvc-3 url


【解决方案1】:

您可以将 id 作为 RedirectToAction() 方法的 routeValues 参数的一部分传递。

return RedirectToAction("Action", new { id = 99 });

这将导致重定向到Site/Controller/Action/99.

编辑: 要自定义 url,您必须创建 custom route 为:

routes.MapRoute(  "Project", // Route name
                   "{controller}/{Action}/{ProjectId}/{MachineName}/{CompanyId}", 
                   new { controller = "Project", 
                         action = "Directives", 
                         ProjectId= 0,
                         MachineName= "",
                         CompanyId = 0 
                       }  // Parameter defaults );

如果您希望任何参数可选,请使用UrlParameter.Optional。 关注 Stephen Walthe 的文章ASP.NET MVC Routing

请参考:RedirectToAction with parameter

【讨论】:

    【解决方案2】:

    尝试自定义路线:

    routes.MapRoute(
         "Project",                                              // Route name
         "{Controller}/{Action}/{projectId}/{machineName}/{companyId}",                           
         new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 }  // Parameter defaults
    );
    

    http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs

    【讨论】:

    • 完美运行。但我无法得到'return RedirectToAction("Directives", "Project", new { MachineName = project.MachineName, ProjectId = projectId, CompanyId = user.Company_Id });` 给我正确的价值?它仍然是 /Dire.../Project=23&CompanyId=27....
    • 试试return RedirectToAction("Directives", "Project", new { projectId = projectId, machineName = project.MachineName, CompanyId = user.Company_Id }); 您可能需要按正确的顺序设置参数?
    猜你喜欢
    • 1970-01-01
    • 2012-07-30
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多