【问题标题】:ActionLink doesn't work动作链接不起作用
【发布时间】:2014-12-24 10:48:45
【问题描述】:

我很确定我在做一些非常愚蠢的事情。请看看,让我知道我做错了什么。

这是我的ActionLink

@Html.ActionLink("Edit","Edit","UserProfile", new { id = Model.ApplicationUserId },null)

当我点击它时,它会抛出错误的请求异常,而且我注意到 url 是

https://localhost:44304/UserProfile/Edit/92b1347c-c9ec-4728-8dff-11bc9f935d0b

不是

https://localhost:44304/UserProfile/Edit?userId=92b1347c-c9ec-4728-8dff-11bc9f935d0b

我的控制器中有一个HTTPGET 编辑方法,它需要UserId。当我手动传递路线值时,它可以工作。请帮助我。 非常感谢您的帮助,总有一天,您会付出代价。 谢谢! 干杯!

【问题讨论】:

  • 在 UserProfile 控制器上发布 Edit 操作方法的代码。不确定参数名称是id 还是userId,还需要知道参数是什么类型。
  • 非常感谢!将其更改为 UserId 完全有效。我认为 'id' 只是一个临时名称,稍后将匹配方法参数中的任何内容。我猜我完全错了。
  • 也放上控制器动作。您不仅没有放置错误,而且还没有显示您的控制器端!

标签: razor asp.net-mvc-5


【解决方案1】:

如果您期望的参数是userId,则使用@Html.ActionLink,如下所示:

@Html.ActionLink("Edit","Edit","UserProfile", new { userId = Model.ApplicationUserId },null)

如果您传递名称为 id 的参数,则 MVC 将像您提到的那样路由:

https://localhost:44304/UserProfile/Edit/92b1347c-c9ec-4728-8dff-11bc9f935d0b

这很好,但是您的方法应该是期望具有适当名称的参数的东西:

// GET: /UserProfile/Edit/{id}
public ActionResult Edit(String id){


     //your code
     return View();
}

如果您有时间,请查看ASP.NET MVC Routing Overview 了解更多详情。

【讨论】:

  • 哇!我认为 'id' 只是一个临时名称,稍后将匹配方法参数中的任何内容。我猜我完全错了,非常感谢:)
  • 当我开始使用 MVC 时,它也发生在我身上:D @RandikaWijekoon 很高兴我能提供帮助
  • 没有反对票 :) 。只是勾号。甚至在 5 分钟之前,我都非常渴望打勾。谢谢大家!
【解决方案2】:

您需要将控制器操作的参数从 userId 编辑为 id - 最佳变体。

public Edit(int id)
{
}

或者

@Html.ActionLink("Edit","Edit","UserProfile", new { userId = Model.ApplicationUserId },null)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2011-07-08
    相关资源
    最近更新 更多