0

I would like to have this url

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

But when i submit the form in the UI, the "&keyword" doesn't appear. And i dont understand why. The URL generated is only this : /Admin/Product?page=1

Product.Controller :

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

This is the form in the UI :

More info : this form has the role to filter data through a keyword submitted by a user by the 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>

This is the entire index.html markup

@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>
8
  • 1
    1) What happens if you enter the /Admin/Product?page=123&keyword=somethingToSearch in your browser? Did this request hit the Index action method with the entered parameters? 2) Can you show code how you render the Index view and full view markup? Commented Aug 30, 2023 at 16:44
  • Am I right that you just don't see &keyword in the URL? Or you don't receive the correct keyword value in the action? Commented Aug 30, 2023 at 18:56
  • If you don't see it in URL, that's because it is in the request body and that's absolutely normal (the keyword parameter will be assigned to the correct value). If you receive empty string in keyword — what does Request.Form["keyword"] return? Commented Aug 30, 2023 at 18:58
  • @Jackdaw If i try to enter the URL written by you, it returns successfull. But i would like to see in the URL the portion of "keyword=..." Commented Aug 31, 2023 at 9:12
  • @SNBS Ok so i don't see the &keyword because it is in the request body. But if I would like to not receive it in the request body but in the query strings? Commented Aug 31, 2023 at 9:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.