问题描述
首先,我想澄清一下,我完全意识到有很多类似的问题,但是没有一个对我有用。
我正在尝试在我的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}
但是控制器中的字符串xid 返回空值。
我在做什么错了?
N.B忽略控制器中的代码,因为xid是完成代码所需要的,所以代码不完整。
解决方法
您可以尝试这样的事情吗?
@Html.ActionLink("My Link","GetImages","MarketPlace" new { id = attachmentToFecth },null)
public async Task<Actionresult> GetImages(string id)
{
// Do stuff with the id
}
也请查看这篇文章的答案