Searching in Asp.Net Core
Package
- "Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0"
- "Microsoft.EntityFrameworkCore.Tools" Version="5.0.0"
- "Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2"
Controllers[Folder]
ProductsController.cs
using Microsoft.AspNetCore.Mvc;
using Searching.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Searching.Controllers
{
public class ProductsController : Controller
{
private readonly ApplicationDbContext _context;
public ProductsController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult Index(string searchProductName=null)
{
if(string.IsNullOrEmpty(searchProductName))
{
return View();
}
var searchResult = _context.Products.Where(x => x.Title.Contains(searchProductName)).ToList();
if(searchResult.Count==0)
{
ViewBag.products = "Search Product Not Found";
return View();
}
return View(searchResult);
}
}
}
Models[Folder]
ApplicationDbContext.cs
using Microsoft.EntityFrameworkCore;
using Searching.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Searching.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext>options):base(options)
{
}
public DbSet<Product> Products { get; set; }
}
}
Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Searching.Models
{
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
Views[Folder]
Products
Index.cshtml
@model IEnumerable<Searching.Models.Product>
@{
ViewData["Title"] = "Index";
}
<h1>Search</h1>
<form asp-action="Index">
<div class="form-group">
<label>Search Product Name:</label>
<input type="text" name="searchProductName" class="form-control" />
<input type="submit" vlaue="Search" />
</div>
@if(ViewBag.products!=null)
{
<span class="alert alert-danger">@ViewBag.products</span>
}
<div>
@if(Model!=null)
{
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
}
</div>
</form>
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=CHETUIWK1689\\MSSQL2019;Database=Search;Trusted_Connection=True;MultipleActiveResultSets=True"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Searching.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Searching
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Products}/{action=Index}/{id?}");
});
}
}
}
0 Comments