I have many users on Admin pannel. i want to search users by the User_Name,Email,GST_Number and Contact Number please give me some solution for this task. using MVC C#..!!!!!!! Thank You
Loading
I have many users on Admin pannel. i want to search users by the User_Name,Email,GST_Number and Contact Number please give me some solution for this task. using MVC C#..!!!!!!! Thank You
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Chetan SanghaniRank #244 · 10.2K reputationPosted Jul 4, 2024, 4:41 AM EDT
To search users by multiple criteria such as User_Name, Email, GST_Number, and Contact Number in an ASP.NET MVC application, you can follow these steps:
Create the Model
First, ensure you have a
Usermodel with the properties User_Name, Email, GST_Number, and Contact Number.public class User { public int Id { get; set; } public string User_Name { get; set; } public string Email { get; set; } public string GST_Number { get; set; } public string ContactNumber { get; set; } }Create the ViewModel
Create a ViewModel to handle the search criteria.
public class UserSearchViewModel { public string UserName { get; set; } public string Email { get; set; } public string GSTNumber { get; set; } public string ContactNumber { get; set; } public List Users { get; set; } }Create the Controller
Create a controller with actions to display the search form and to process the search query.
public class UserController : Controller { private readonly ApplicationDbContext _context; public UserController() { _context = new ApplicationDbContext(); } public ActionResult Index() { var model = new UserSearchViewModel { Users = new List() }; return View(model); } [HttpPost] public ActionResult Index(UserSearchViewModel model) { var users = _context.Users.AsQueryable(); if (!string.IsNullOrEmpty(model.UserName)) { users = users.Where(u => u.User_Name.Contains(model.UserName)); } if (!string.IsNullOrEmpty(model.Email)) { users = users.Where(u => u.Email.Contains(model.Email)); } if (!string.IsNullOrEmpty(model.GSTNumber)) { users = users.Where(u => u.GST_Number.Contains(model.GSTNumber)); } if (!string.IsNullOrEmpty(model.ContactNumber)) { users = users.Where(u => u.ContactNumber.Contains(model.ContactNumber)); } model.Users = users.ToList(); return View(model); } }Create the View
Create a view (e.g.,
Index.cshtml) to display the search form and the results.@model YourNamespace.Models.UserSearchViewModel @{ ViewBag.Title = "User Search"; } User Search @using (Html.BeginForm()) { @Html.LabelFor(m => m.UserName) @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) @Html.LabelFor(m => m.Email) @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) @Html.LabelFor(m => m.GSTNumber) @Html.TextBoxFor(m => m.GSTNumber, new { @class = "form-control" }) @Html.LabelFor(m => m.ContactNumber) @Html.TextBoxFor(m => m.ContactNumber, new { @class = "form-control" }) Search } @if (Model.Users.Any()) { Search Results User Name Email GST Number Contact Number @foreach (var user in Model.Users) { @user.User_Name @user.Email @user.GST_Number @user.ContactNumber } } else { No users found. }Database Context
Ensure your
ApplicationDbContextincludes aDbSetfor your users.public class ApplicationDbContext : DbContext { public DbSet Users { get; set; } }With these steps, you will have a functional search feature in your ASP.NET MVC application that allows you to search users by User_Name, Email, GST_Number, and Contact Number.