[英]ActionResult not correctly getting parameter
In my Home/Index view I have a list view which displays cards with the photo and details of all uploads each containing an action link on click for each card:在我的主页/索引视图中,我有一个列表视图,其中显示卡片,其中包含所有上传的照片和详细信息,每个卡片都包含每张卡片的点击操作链接:
@model IEnumerable<FunPetPics.Models.PetPhotoModel>
@{
ViewData["Title"] = "Home Page";
}
@if (Model == null || !Model.Any())
{
<p>uploads will be displayed here</p>
}
else
{
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
@await Html.PartialAsync("_PetPhotoModelFilter")
}
<div class="card-columns">
@foreach (var upload in Model)
{
<a href='@Url.Action( "Rate", "Details", upload.Id )'>
<div class="card">
<img class="card-img-top" src="@("~/UploadedPhotos/"+ upload.ImageName)" asp-append-version="true">
<div class="card-body">
<h4 class="card-title">@upload.Title</h4>
<h6 class="card-subtitle mb-2 text-muted">@upload.PetName</h6>
<p class="card-text">@upload.DateUploaded</p>
<p class="card-text">@upload.Description</p>
<p class="card-subtitle">Ratings:</p>
<p class="card-text">
Cute:@upload.AverageCutenessRating/5<br />
Funny:@upload.AverageFunnynessRating/5<br />
Awsome:@upload.AverageAwsomnessRating/5<br />
</p>
</div>
</div>
</a>
}
</div>
DetailsControler:细节控制器:
using FunPetPics.Data;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace FunPetPics.Controllers
{
public class DetailsController : ControllerBase
{
public DetailsController(FunPetPicsContext context) : base(context)
{
}
[HttpGet]
//[Route("Details/Rate/{id}")] when I add this the controller doesn't work and I get page not found
public IActionResult Rate(int id)
{
var model = _context.PetPhotos.FirstOrDefault(p => p.Id == id);
return View(model);
}
}
}
For now the Details/Rate view is meant to just display the photo until I can get the rest working:现在,详细信息/价格视图仅显示照片,直到我可以让 rest 正常工作:
@using Microsoft.AspNetCore.Http;
@model PetPhotoModel
@{
<img src="@("~/UploadedPhotos/"+ ((PetPhotoModel)Model).ImageName)" asp-append-version="true">
}
When the link displayed from the Home/Index page displayed on the browser it takes me to https://localhost:44316/Rate/Details
(why is there no id?) and the id
in the controler is 0 (not the correct one).当浏览器上显示的主页/索引页面显示链接时,它会将我带到
https://localhost:44316/Rate/Details
(为什么没有 id?)并且控制器中的id
为 0(不是正确的)。
I have also tried adding this attribute to the controller's Rate()
method: [Route("Details/Rate/{id}")]
but no breakpoints are hit in the controller and it displays page not found with the same URL.我还尝试将此属性添加到控制器的
Rate()
方法: [Route("Details/Rate/{id}")]
但在 controller 中没有遇到断点,并且它显示的页面未找到具有相同的 URL。
You are not using Url.Action
correctly.您没有正确使用
Url.Action
。 Leave the Route
attribute in, and change your view to use this instead, note how it uses an anonymous object to allow MVC to link the id
value to a matching parameter:保留
Route
属性,并更改视图以使用它,注意它如何使用匿名 object 来允许 MVC 将id
值链接到匹配参数:
@Url.Action( "Rate", "Details", new { id = upload.Id })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.