How i can perform search operation in mvc

Total Post:13

Points:91
Mvc4 
 387  View(s)
Ratings:
Rate this:
Hi friends, I want to perform search operation in mvc application.But i can not do this.
 please give me an idea..
 
  1. Post:9

    Points:63
    Re: How i can perform search operation in mvc

    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>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!