---
title: "How i can perform search operation in mvc"  
description: "How i can perform search operation in mvc"  
author: "Samuel Fernandes"  
published: 2017-08-24  
updated: 2023-06-18  
canonical: https://www.mindstick.com/forum/34332/how-i-can-perform-search-operation-in-mvc  
category: "asp.net mvc"  
tags: ["mvc4"]  
reading_time: 4 minutes  

---

# How i can perform search operation in mvc

Hi [friends](https://answers.mindstick.com/qa/31396/where-did-the-suggest-to-friends-feature-go), I want to perform [search](https://www.mindstick.com/articles/65368/best-smo-services-company-in-hyderabad-improve-search-rankings) [operation](https://www.mindstick.com/forum/33917/how-to-callback-operation-using-delegate-in-c-sharp) in [mvc application](https://www.mindstick.com/forum/12932/how-to-consume-webservices-from-asp-dot-net-mvc-application).But i can not do this.\
please give me an idea..\
\

## Replies

### Reply by Aryan Kumar

Sure, here is how you can perform search operation in [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc):

1. Create a model class that represents the data that you want to search.
2. Create a controller action that gets the data from the model and returns it as a JSON object.
3. Create a view that renders the search results and passes the JSON object to the view.
4. Use LINQ to filter the data based on the search criteria.

Here is an example of how you can perform search operation in MVC:

C#

```plaintext
public class ProductModel
{
  public int ProductId { get; set; }
  public string ProductName { get; set; }
  public int QuantitySold { get; set; }
}

public class ProductController : Controller
{
  public ActionResult Search(string searchTerm)
  {
    // Get the products from the database.
    var products = new List<ProductModel>();
    products.Add(new ProductModel { ProductId = 1, ProductName = "Product 1", QuantitySold = 10 });
    products.Add(new ProductModel { ProductId = 2, ProductName = "Product 2", QuantitySold = 20 });
    products.Add(new ProductModel { ProductId = 3, ProductName = "Product 3", QuantitySold = 30 });

    // Filter the products based on the search term.
    var filteredProducts = products.Where(p => p.ProductName.Contains(searchTerm));

    // Return the filtered products as a JSON object.
    return Json(filteredProducts);
  }
}
```

This code will create a model class that represents the product data. The model class has three properties: ProductId, ProductName, and QuantitySold.

The controller action `Search()` gets the products from the database and filters the products based on the search term. The search term is passed to the controller action as a parameter.

The view that renders the search results should have a search box that allows the user to enter the search term. The view should also have a list of the products that match the search term.

For example, the following code shows how to render the search results in a view:

HTML

```plaintext
<input type="text" name="searchTerm" />
<button>Search</button>

<ul>
  @foreach (var product in Model) {
    <li>
      <a href="/ProductDetails/{{ product.ProductId }}">
        {{ product.ProductName }}
      </a>
    </li>
  }
</ul>
```

This code will create a search box that allows the user to enter the search term. The search results will be displayed as a list of links. The links will point to the product details page for each product.

### Reply by Niraj Kumar Mishra

i don't know what code you want means View or Controller. So i write both code . i hope it will help you.\
**Controller code:\**

```
namespace CompanyDemo.Controllers{    public class EmployeeController : Controller    {        private SampleDbContext2 db = new SampleDbContext2();        //        // GET: /Employee/        public ActionResult Index(string SearchBy,string Search)        {            if (SearchBy == "Name")            {                return View(db.Employees.Where(x => x.Name.Contains(Search) || Search == null).ToList());            }            else if (SearchBy == "City")            {                return View(db.Employees.Where(x => x.City.Contains(Search) || Search == null).ToList());            }            else if (SearchBy == "Mobile")            {                return View(db.Employees.Where(x => x.Mobile.Contains(Search) || Search == null).ToList());            }            else            {                var employees = db.Employees.Include(e => e.Company).Include(e => e.EmployeeType);                return View(employees.ToList());            }                    }     } }
```

**View code:\**

```
@model IEnumerable<CompanyDemo.Models.Employee>@{    ViewBag.Title = "Index";}<div class="container">    <div class="row">        <div class="col-md-2" style="margin-left:10px">          <label>Search By</label>        </div>        @using (Html.BeginForm("Index", "Employee", FormMethod.Post))     {                <div class="col-md-2">                   @Html.DropDownList("SearchBy", new List<SelectListItem>                 {                     new SelectListItem{Text="Name", Value="Name", Selected=true},                    new SelectListItem{Text="City",Value = "City" },                    new SelectListItem{Text="Mobile",Value = "Mobile" },                                                                            }, new { @class = "form-control" })        </div>        <div class="col-md-2">         <input type="text" name="search" class="form-control" placeholder="Search..">        </div>        <div class="col-md-1">            <input type="submit" value="Search" class="btn btn-success" />        </div>      }                  </div></div>
```

\


---

Original Source: https://www.mindstick.com/forum/34332/how-i-can-perform-search-operation-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
