C# 通过asp.net核心mvc中的@Html.ActionLink传递参数
首先,我想澄清的是,我完全知道有许多类似的问题,但没有一个对我有用 我正在尝试在我的ASP.NET Core 3.1 MVC应用程序中使用user@Html.ActionLink 我正在从控制器接收id,并将其保存在视图中的变量中C# 通过asp.net核心mvc中的@Html.ActionLink传递参数,c#,asp.net-core-mvc,asp.net-core-3.1,html.actionlink,C#,Asp.net Core Mvc,Asp.net Core 3.1,Html.actionlink,首先,我想澄清的是,我完全知道有许多类似的问题,但没有一个对我有用 我正在尝试在我的ASP.NET Core 3.1 MVC应用程序中使用user@Html.ActionLink 我正在从控制器接收id,并将其保存在视图中的变量中 @{ string attachmentToFecth = Model.AttachmentId; } 稍后,我尝试通过ActionLink将其传递给我的控制器,以获取类似这样的数据 @Html.ActionLink("", "
@{
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);
}
我的控制器函数如下所示
@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);
}
[HttpGet]
公共操作结果GetImages(字符串xid)
{
列表图像=新列表();
var userID=\u applicationUser.Users.FirstOrDefault(obj=>obj.UserName==User.Identity.Name).userID;
var displayImages=(来自_context.Attachments中的图像
在图像上的_context.InfoProducts中加入imageID。AttachmentId等于imageID.AttachmentId
选择新的
{
images.AttachmentPath
});
返回视图(显示图像);
}
当我在ActionLink上放置断点时,我可以看到预期的id正在接收中
新{xid=attachmentToFecth}
但是控制器中的字符串xid正在返回null值
我做错了什么
注意:忽略控制器内部的代码,它是不完整的,因为需要xid来完成它。您能试试这样的吗
@Html.ActionLink("My Link", "GetImages","MarketPlace" new { id = attachmentToFecth }, null)
public async Task<Actionresult> GetImages(string id)
{
// Do stuff with the id
}
@Html.ActionLink(“我的链接”、“GetImages”、“MarketPlace”新{id=attachmentToFecth},空)
公共异步任务操作的路径是什么?如果它是默认的{controller}/{action}/{id?}
,则路由需要名为id
的参数,而不是xid
。因此,它忽略xid
参数。它使用默认路由。将参数作为id而不是xid传递也不起作用。它仍然接收空值。您是否在浏览器中按F12键检查生成的url?它是什么样子的?它在我的项目中可以很好地工作。您的操作名称是GetImages,但您在锚定中使用了详细信息。我建议您可以尝试创建一个新项目并测试它是否工作。如果仍然不工作,你能把新的回购协议分享给我们吗?我已经检查了你提供的链接。对我来说,我的代码看起来和这里给出的答案一样,但不知何故它仍然不起作用。但是,如果您不介意的话,请详细说明将其更改为异步将如何帮助我的原因。从示例代码中,您从ActionLink调用“Details”操作,但您提供了GetImages方法。这只是一个示例还是一个真正的代码?同时检查控制器名称,“MarketPlaceController”看起来正常。哦,是的。我现在明白了。我正在改变这一点并再次尝试。我解决了这个问题,但我没有使用action link,而是使用ajax请求从数据库中获取数据。恭喜@Mill3r