ASP.NET Core MVC Web App 不在 Id 前附加斜杠

ASP.NET Core MVC Web App does not append slash before Id

我正在观看一些教程来学习 ASP.NET Core.I 正在创建一个 MVC Web App.In 网站 我有 products.When 的编辑选项 我点击编辑按钮就可以了为此 url ;

"https://localhost:5001/Admin/Products/Edit1"

它给了我 error.Instead 它应该去

"https://localhost:5001/Admin/Products/Edit/1"

但是确实如此 not.How 我可以解决这个问题吗?

Product/Index.cshtml

@model IEnumerable<NewGraniteHouse.Models.Products>
@{
ViewData["Title"] = "Index";
}

<br /><br />

<div class="row">
    <div class="col-6">
        <h2 class="text-info">Product List</h2>
    </div>
    <div class="col-6 text-right">
        <a asp-action="Create" class="btn btn-info"><i class="fas fa-plus"></i>&nbsp; New Product</a>
    </div>
</div>

<br />
<div>
    <table class="table table-striped border">
        <tr class="table-info">
            <th>
                @Html.DisplayNameFor(m => m.Name)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.Price)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.Available)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.ProductTypes)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.SpecialTags)
            </th>
            <th></th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
        <tr>
            <td>
                @Html.DisplayFor(m => item.Name)
            </td>
            <td>
                @Html.DisplayFor(m => item.Price)
            </td>
            <td>
                @Html.DisplayFor(m => item.Available)
            </td>
            <td>
                @Html.DisplayFor(m => item.ProductTypes.Name)
            </td>
            <td>
                @Html.DisplayFor(m => item.SpecialTags.Name)
            </td>
            <td>
                <partial name="_TableButtonPartial" model="item.Id" />
            </td>
        </tr>
        }
    </table>
</div>

Product/Edit.cshtml

@model NewGraniteHouse.Models.ViewModel.ProductsViewModel
@using NewGraniteHouse.Extensions
@{
ViewData["Title"] = "Edit";
}

<br />
<h2 class="text-info">Edit Product</h2>

<form method="post" enctype="multipart/form-data">
    <div class="p-4 border rounded row">
        <input hidden asp-for="Products.Id" />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="col-8">
            <div class="form-group row">
                <div class="col-4">
                    <label asp-for="Products.Name"></label>
                </div>
                <div class="col-8">
                    <input asp-for="Products.Name" class="form-control" />
                </div>
                <span asp-validation-for="Products.Name" class="text-danger"></span>
            </div>
            <div class="form-group row">
                <div class="col-4">
                    <label asp-for="Products.Price"></label>
                </div>
                <div class="col-8">
                    <input asp-for="Products.Price" class="form-control" />
                </div>
                <span asp-validation-for="Products.Price" class="text-danger"></span>
            </div>
            <div class="form-group row">
                <div class="col-4">
                    <label asp-for="Products.Image"></label>
                </div>
                <div class="col-8">
                    <input type="file" name="files" class="form-control" />
                </div>
            </div>
            <div class="form-group row">
                <div class="col-4">
                    <label asp-for="ProductTypes"></label>
                </div>
                <div class="col-8">
                    <select asp-for="@Model.Products.ProductTypeId" asp-items="Model.ProductTypes.ToSelectListItem(Model.Products.ProductTypeId)"
                        class="form-control"></select>
                </div>

            </div>
            <div class="form-group row">
                <div class="col-4">
                    <label asp-for="SpecialTags"></label>
                </div>
                <div class="col-8">
                    <select asp-for="@Model.Products.SpecialTagsId" asp-items="Model.SpecialTags.ToSelectListItem(Model.Products.SpecialTagsId)"
                        class="form-control"></select>
                </div>

            </div>
            <div class="form-group row">
                <div class="col-4">
                    <label asp-for="Products.ShadeColor"></label>
                </div>
                <div class="col-8">
                    <input asp-for="Products.ShadeColor" class="form-control" />
                </div>
                <span asp-validation-for="Products.ShadeColor" class="text-danger"></span>
            </div>
            <div class="form-group row">
                <div class="col-4">
                    <label asp-for="Products.Available"></label>
                </div>
                <div class="col-8">
                    <input type="checkbox" asp-for="Products.Available" />
                </div>
            </div>
        </div>
        <div class="col-1">

        </div>
        <div class="col-3">
            <img src="@Model.Products.Image" width="100%" style="border-radius:5px; border:1px solid #bbb9b9; " />
        </div>
        <br />
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Update" />
            <a asp-action="Index" class="btn btn-success">Back to List</a>
        </div>
    </div>
</form>

@section Scripts{
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

_TableButtonPartial.cshtml

@model int
@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@

<td style="width:150px">
    <div class="btn-group" role="group" ">
        <a type="button" class="btn btn-primary" href="@Url.Action("Edit"+Model)">
            <i class="fas fa-edit"></i>
        </a>
        <a type="button" class="btn btn-success" href="@Url.Action("Details"+Model)">
            <i class="fas fa-list-ul"></i>
        </a>
        <a type="button" class="btn btn-danger" href="@Url.Action("Delete"+Model)">
            <i class="fas fa-trash-alt"></i>
        </a>
    </div>
</td>

编辑:添加了 cshtml 文件。

从你的部分观点来看,你似乎必须将 Id 作为参数传递给你的控制器方法,但你将其写为

@Url.Action("Edit"+Model)

但是满足您要求的最佳方法重载是 @Url.Action(string actionName, object routeValues);

所以现在您的操作 link 将被修改,看起来像。

@Url.Action("Edit", new {id = Model})

现在你的局部视图看起来像

@model int
@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@

<td style="width:150px">
    <div class="btn-group" role="group" ">
        <a type="button" class="btn btn-primary" href="@Url.Action("Edit", new { id = Model})">
            <i class="fas fa-edit"></i>
        </a>
        <a type="button" class="btn btn-success" href="@Url.Action("Details", new { id = Model})">
            <i class="fas fa-list-ul"></i>
        </a>
        <a type="button" class="btn btn-danger" href="@Url.Action("Delete", new { id = Model})">
            <i class="fas fa-trash-alt"></i>
        </a>
    </div>
</td>

试试这个可能对你有帮助。