I have an asp.net core mvc project, write me a view page for displaying a list of…
- User
I have an asp.net core mvc project, write me a view page for displaying a list of products coming from the dbcontext. Also i need you to make a search functionality withing the page. The searchbar should neglect and "display:none" any unmatching product. The search should be by the name and it all runs at realtime without reloading the page. The page should be responsive using tailwindcss. you can use this for the searchbar theme: "
<!--Search icon--> <span class="input-group-text flex items-center whitespace-nowrap rounded px-3 py-1.5 text-center text-base font-normal text-neutral-700 dark:text-neutral-200" id="basic-addon2"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="h-5 w-5"> <path fill-rule="evenodd" d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z" clip-rule="evenodd" /> </svg> </span> - Assistant
To create a view page for displaying a list of products from the dbcontext and adding search functionality, follow these steps:
- In your
Viewsfolder, create a new folder calledProducts. - Inside the
Productsfolder, create a new file calledIndex.cshtml. - Add the following code in
Index.cshtml:
@model IEnumerable<Product> <!-- Assuming your model class is named `Product` --> @{ ViewData["Title"] = "Products"; } <div class="container mx-auto px-4"> <div class="mb-3"> <div class="relative mb-4 flex w-full flex-wrap items-stretch"> <input id="searchInput" type="search" class="relative m-0 block w-[1px] min-w-0 flex-auto rounded border border-solid border-neutral-300 bg-transparent bg-clip-padding px-3 py-[0.25rem] text-base font-normal leading-[1.6] text-neutral-700 outline-none transition duration-200 ease-in-out focus:z-[3] focus:border-primary focus:text-neutral-700 focus:shadow-[inset_0_0_0_1px_rgb(59, 113, 202)] focus:outline-none dark:border-neutral-600 dark:text-neutral-200 dark:placeholder:text-neutral-200 dark:focus:border-primary" placeholder="Search" aria-label="Search" aria-describedby="basic-addon2" /> <span class="input-group-text flex items-center whitespace-nowrap rounded px-3 py-1.5 text-center text-base font-normal text-neutral-700 dark:text-neutral-200" id="basic-addon2"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="h-5 w-5"> <path fill-rule="evenodd" d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z" clip-rule="evenodd" /> </svg> </span> </div> </div> <div id="productList" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> @foreach (var item in Model) { <div data-name="@item.Name" class="card"> <div class="flex flex-col items-center p-4 m-2 bg-white shadow-md rounded-lg"> <img class="w-full h-64 rounded-md object-cover" src="@item.ImageUrl" alt="@item.Name" /> <h2 class="mt-4 text-xl font-semibold">@item.Name</h2> <p class="text-gray-600 mt-1">Price: @item.Price </p> </div> </div> } </div> </div> @section Scripts { <script> document.addEventListener("DOMContentLoaded", function() { var searchInput = document.getElementById("searchInput"); searchInput.addEventListener("input", function() { var searchTerm = searchInput.value.toLowerCase(); var productList = document.getElementById("productList"); var products = productList.querySelectorAll(".card"); products.forEach(product => { var productName = product.getAttribute("data-name").toLowerCase(); if (productName.includes(searchTerm)) { product.style.display = "block"; } else { product.style.display = "none"; } }); }); }); </script> }Make sure to update the
@modeldeclaration at the top of the file with the correct namespace and class name for yourProductmodel.This code will create a view with a search bar and a responsive grid layout to display the products. The search functionality is implemented using JavaScript, and it will filter the displayed products in real-time based on the user's input without reloading the page.
- In your