mvc 路由的多个参数

问题描述 投票:0回答:1

我想要这个网址

/Admin/Product?page=1&keyword=somethingToSearch

但是当我在用户界面中提交表单时,“&keyword”不会出现。我不明白为什么。生成的网址只有这个:

/Admin/Product?page=1

Product.Controller :

public IActionResult Index(int page = 1, string keyword = "")

这是 UI 中的表单:

更多信息:此表单的作用是通过用户提交的关键字过滤数据,

input name="keyword"

 <form asp-action="Index" class="d-flex">

     <input type="hidden" name="page" [email protected]["page"] />

     <input name="keyword"  class="form-control me-2 w-50" type="search" placeholder="Search" aria-label="Search">
     <button class="btn btn-outline-success" type="submit">Search</button>
 </form>

这是整个index.html 标记

@using System.Reflection
@using Bulky.Models.Attributes;
@using System.ComponentModel.DataAnnotations;
@using System.ComponentModel

@{
    var props = typeof(ProductVM2).GetProperties();
}

@model PaginatedList<ProductVM2>

<div class="card shadow border-0 mt-4">
    <div class="card-header bg-secondary bg-gradient ml-0 py-3">
        <div class="row">
            <div class="col-12 text-center">
                <h2 class="text-white py-2">Product List</h2>
            </div>
        </div>
    </div>
    <div class="card-body p-4">

        <div class="row pt-4 pb-3">
            <div class="col-6">
                <a asp-controller="Product" asp-action="Upsert" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i>
                    Create New Product
                </a>
            </div>
        </div>    
        
        <div class="row pt-4 pb-3">

            @{
                var pageQuery = Context.Request.Query["page"];
            }

            <input 
                name="keyword"
                onchange="SubmitFiltering(@pageQuery, this.value)"
                class="form-control me-2 w-50"
                type="search"
                placeholder="Search"
                aria-label="Search"
            >

            <button class="btn btn-outline-success w-50" type="submit">Search</button>
        </div>

        <table class="table">
            <thead>
                <tr>
                    @foreach(var prop in props)
                    {
                        if(prop.GetCustomAttribute<HideProperty>() == null)
                        {
                            <th>

                                @(prop.GetCustomAttribute<DisplayNameAttribute>(true) != null ? prop.GetCustomAttribute<DisplayNameAttribute>(true).DisplayName : prop.Name)

                            </th>
                        } 
                    }
                   
                    <th scope="col"></th>
                </tr>
            </thead>
            <tbody>
                @if(Model.Count != 0 && Model != null)
                {
                    @foreach (ProductVM2 item in Model.ToList())
                    {
                        <tr>
                            <td>@item.Title</td>
                            <td>@item.ISBN</td>
                            <td>@item.ListPrice</td>
                            <td>@item.Author</td>
                            <td>@item.Category.Name</td>
                            <td>

                                <a class="btn btn-primary" href="/admin/product/[email protected]">
                                    <i class="bi bi-pencil-square"></i> Edit
                                </a>
                                <a class="btn btn-danger" href="/admin/product/[email protected]">
                                    <i class="bi bi-trash-fill"></i> Remove
                                </a>
                            </td>
                        </tr>
                    }
                }
                
            </tbody>
        </table>

        <nav aria-label="...">
            <ul class="pagination">

                <li class="page-item @(!Model.HasPreviousPage ? "disabled" : "")">
                    <a asp-route-page=@(Model.PageIndex - 1) class="page-link" tabindex="-1" aria-disabled="true">
                        Previous
                    </a>
                </li>

                @if (Model.HasPreviousPage)
                {
                    <li class="page-item">
                        <a class="page-link"
                           asp-route-page=@(Model.PageIndex - 1)>
                            @(Model.PageIndex - 1)
                        </a>
                    </li>
                }

                <li class="page-item active" aria-current="page">
                    <a class="page-link" href="#">@Model.PageIndex</a>
                </li>

                @if (Model.HasNextPage)
                {
                    <li class="page-item">
                        <a
                            class="page-link"
                            asp-route-page=@(Model.PageIndex + 1)>
                            @(Model.PageIndex + 1)
                        </a>
                    </li>
                }
                <li class="page-item @(!Model.HasNextPage ? "disabled" : "")">
                    <a asp-route-page=@(Model.PageIndex + 1) class="page-link" >
                        Next
                    </a>
                </li>

            </ul>
        </nav>

    </div>

</div>
asp.net-mvc routes filtering query-string url-parameters
1个回答
0
投票

这不是答案:我只是不知道如何在评论中附加屏幕截图。

当我在测试应用程序的浏览器中输入

https://localhost:7208/Admin/Product?page=123&keyword=somethingToSearch
URL 时,我在渲染
Index
视图后看到以下内容:

你也看到同样的情况吗?

© www.soinside.com 2019 - 2024. All rights reserved.