Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing search in Asp.net MVC

I am new to Asp.net MVC and have no idea as to how can i perform the search. Here's my requirement, please tell me how will you handle this :-

I need to have textbox where user can enter a search query or string. The user then clicks on a button or presses enter to submit it. The string needs to matched with a table's property name.

NOTE:- Querying the data and fetching the result isn't the main point here. All I need to know is how will you take the user input and pass it to a controller action or whatever for further processing. Just tell me how will you read the user input and where will you send it to search.

like image 401
Pankaj Upadhyay Avatar asked Nov 17 '11 13:11

Pankaj Upadhyay


People also ask

How to perform search operation in ASP net MVC?

Step 1: Go to File, New, then Project. Step 2: Go to File, New, Project. Choose "ASP.NET MVC 4 Web Application" from the list, then provide the application name as you wish and set the path in the location input where you want to create the application. Step 3: Now choose the Project Template "Basic".

What is PagedList MVC?

PagedList. mvc is a package for paging and sorting for ASP.NET MVC. PagedList package installs a PagedList collection type and extension methods for IQueryable and IEnumerable collections. Table Data. Open a New Project.

How to use search function in ASP net?

The Search functionality will be implemented using a Stored Procedure which will be called by passing the parameter value using Entity Framework in ASP.Net MVC 5 Razor. In this article I will explain with an example, how to implement Search functionality using Entity Framework in ASP.Net MVC 5 Razor.


1 Answers

As always in an ASP.NET MVC application you start by defining a view model which will express the structure and requirements of your view. So far you have talked about a form containing a search input:

public class SearchViewModel
{
    [DisplayName("search query *")]
    [Required]
    public string Query { get; set; }
}

then you write a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new SearchViewModel());
    }

    [HttpPost]
    public ActionResult Index(SearchViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // There was a validation error => redisplay the view so 
            // that the user can fix it
            return View(model);
        }

        // At this stage we know that the model is valid. The model.Query
        // property will contain whatever value the user entered in the input
        // So here you could search your datastore and return the results

        // You haven't explained under what form you need the results so 
        // depending on that you could add other property to the view model
        // which will store those results and populate it here

        return View(model);
    }
}

and finally a view:

@model SearchViewModel

@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.Query)
    @Html.EditorFor(x => x.Query)
    @Html.ValidationMessageFor(x => x.Query)
    <button type="submit">Search</button>
}
like image 72
Darin Dimitrov Avatar answered Nov 03 '22 15:11

Darin Dimitrov