简体   繁体   中英

Routing issue after changing primary keys

After changing primary key from ID to PostId , it seems that the Post Details.cshtml view does not render the URL correctly.

If I navigate to https://localhost:xxxxx/Posts/Details/4 , the page renders correctly. However, the @Html.ActionLink call in the Index.cshtml view that was working previously now no longer works. It points to https://localhost:xxxxx/Posts/Details?PostId=4 , which is a blank page. It seems like that URL should work, though, so I'm not sure whether the routing is the issue or it's something to do with the view. The same issue is occurring with a separate class (which also had its primary key changed).

The <a asp-action="Details" asp-route-id="@item.PostId">Details</a> link in the Index.cshtml view does work by pointing to https://localhost:xxxxx/Posts/Details/4 .

Index.cshtml

@model IEnumerable<Website.Models.Post>

<table class="table">
    <thead>
        <tr>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData["TitleSortParm"]">@Html.DisplayNameFor(model => model.Title)</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData["AuthorSortParm"]">@Html.DisplayNameFor(model => model.Author.LastName)</a>
            </th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.ActionLink(item.Title, "Details", "Posts", new { item.PostId })
                </td>
                <td>
                    @Html.ActionLink(item.Author.FullName, "Details", "Authors", new { item.Author.AuthorId })
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.PostId">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.PostId">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.PostId">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

PostsController.cs (relevant Details get method only)

namespace Website.Controllers
{
    public class PostsController : Controller
    {
        private readonly Website.Data.ApplicationDbContext _context;

        public PostsController(Website.Data.ApplicationDbContext context)
        {
            _context = context;
        }


        // GET: Post/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var post = await _context.Post
                .Include(p => p.Author)
                .FirstOrDefaultAsync(m => m.PostId == id);
            if (post == null)
            {
                return NotFound();
            }

            return View(post);
        }
    }
}

Post.cs

namespace Website.Models
{
    public class Post   // dependent on Author
    {
        public int PostId { get; set; } // primary key
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime PublicationDate { get; set; }

        public int AuthorId { get; set; }   // author foreign key
        public Author Author { get; set; }  // author navigation property

        public ICollection<Tag> Tags { get; set; }
    }
}

In Startup.cs, routing info (never changed this, just default):

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

Your action parameter named id . When you use the point with named parameters like this https://localhost:xxxxx/Posts/Details?PostId=4 MVC tries to serialize the PostId to the parameter with the same name. You can rename your parameter or use prefix.

Task<IActionResult> Details([Bind(Prefix="PostId")] int? 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