简体   繁体   中英

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:

@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).

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.

You are not using Url.Action correctly. 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:

@Url.Action( "Rate", "Details", new { id = upload.Id })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM