【发布时间】:2021-01-21 01:16:44
【问题描述】:
首先,我想澄清一下,我完全知道有很多类似的问题,但没有一个对我有用。
我正在尝试在我的 ASP.NET Core 3.1 MVC 应用程序中使用 @Html.ActionLink。
我从控制器接收一个 id 并将其保存在我的视图中的变量中
@{
string attachmentToFecth = Model.AttachmentId;
}
稍后我试图通过 ActionLink 将它传递给我的控制器以获取一些这样的数据
@Html.ActionLink("", "Details", "MarketPlace",
new { xid = attachmentToFecth }, null)
我的控制器功能是这样的
[HttpGet]
public ActionResult GetImages(string xid)
{
List<string> Images = new List<string>();
var userID = _applicationUser.Users.FirstOrDefault(obj => obj.UserName == User.Identity.Name).UserId;
var displayImages = (from images in _context.Attachments
join imageID in _context.InfoProducts on images.AttachmentId equals imageID.AttachmentId
select new
{
images.AttachmentPath
});
return View(displayImages);
}
当我在 ActionLink 处设置断点时,我可以看到预期的 id 正在接收中
新的 { xid = attachmentToFecth }
但是控制器中的 string xid 正在返回 null 值。
我在这里做错了什么?
注意忽略控制器内部的代码,它不完整,因为需要 xid 来完成它。
【问题讨论】:
-
GetImages操作的路径是什么?如果它是默认的{controller}/{action}/{id?}',则路由需要名为id的参数,而不是xid。因此它忽略了xid参数。 -
它使用默认路由。并将参数作为 id 而不是 xid 传递也不起作用。它仍然收到 null。
-
您是否在浏览器中按 F12 以检查生成的 url?它是什么样的?它可以在我的项目中运行良好。并且您的操作名称是 GetImages,但您在锚点中使用了详细信息。我建议您可以尝试创建一个新项目并测试它是否有效。如果仍然无效,请您将新的 repo 分享给我们吗?
标签: c# asp.net-core-mvc asp.net-core-3.1 html.actionlink