【问题标题】:Extend @Url.Action to pass multiple actions扩展 @Url.Action 以传递多个操作
【发布时间】:2016-11-30 13:20:45
【问题描述】:

我正在尝试使用 @Url.Action 通过 URL 传递多个参数,这样我可以使用 RouteAttribute 读取额外的参数

<li><a class="survey-button" href="
@Url.Action("Survey",(Model.SurveyID).ToString(),"Page"(Model.PageNumber).ToString())
">Previous</a></li>

//localhost:8080/Survey/1/Page/1
[Route("Survey/{surveyID}/Page/{pageNumber}")]
public ActionResult Page(int surveyID, int pageNumber)
{
...

我意识到我可以通过查询字符串传递这些,但出于清洁的原因,这不是首选。

【问题讨论】:

    标签: c# asp.net asp.net-mvc razor


    【解决方案1】:

    您需要将参数包装在 RouteValueDictionary 或匿名对象中(更简单)。我假设您的控制器名称是SurveyController

    @Url.Action("Page", "Survey", new {surveyId = Model.SurveyId, pageNumber = Model.PageNumber})
    

    【讨论】:

    • 如果 OP 的控制器名称是 SurveyController,那么 Url.Action 的语法不正确。您需要将 "Page""Survey" 交换。actionNamecontrollerName 之前
    • @BviLLe_Kid 你是对的,我的错。固定的。我被智能感知宠坏了。
    • @DPac,如果我传入您在上面提供的代码 sn-p(@Url.Action("Page", "Survey", new {surveyId = Model.SurveyID, pageNumber = Model.SurveyPageNumber })),我得到一个带有查询字符串 (/Page/1?surveyId=1) 的 url,这是我试图避免的。
    • Intellisense 往往对每个人都有这种影响哈哈。
    • @msio - 那是因为您的 URL 是从 不是 您问题中的路由生成的。与Url.Action 中传递的路由值匹配的第一个路由总是获胜。最好的猜测是您在注册其他路由之前未能通过调用routes.MapMvcRouteAttributes() 启用属性路由,但您也可能有另一个路由值匹配的路由,这也可能是原因。
    猜你喜欢
    • 2017-10-30
    • 1970-01-01
    • 2018-07-15
    • 2015-06-18
    • 2022-11-02
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多