Database Reference
In-Depth Information
In your browser, the page should appear similar to the one shown in Figure 4-15 .
Figure 4-15. The rendered view shown in a browser
How It Works
In the first section of the page (see Listing 4-5), we format the query fields using a table. There's nothing fancy
hereā€”the idea is to provide some structure to capture the three query fields: Name, City, and State. These values, or
the lack of them, will be used in the Controller's Search action method after the Search button is clicked. Thus these
parameters will form the filter for the query.
Next we use an HTML helper to show the result set using WebGrid control. The data source will be the view
model. Take note here that we have created two models: one to fetch the data from the database and one to be used
as a model for the view that will capture the query parameters from the page and also show the customer records.
Actually, the first model will be created at the moment we generate the entity data model for Customer table.
We are using Linq-to-entities to query the Customer entity data model. The where clause and parameter
variables define the filter of our query. In the view, we map the parameters of the search query to the Name, City, and
State HTML helper text boxes. We map the Name property of the model to the Name text box, and so on.
We use a WebGrid to display the results. The WebGrid is bound with the Customer model list, which is a model
that is capturing only the search results.
The controller code, shown in Listing 4-6, is used fetch the results from database and fill the view the first time
view is rendered and also when the Search button is clicked. We have used an .mdf file local database and filled the
records in Customer table.
4-3. Filtering with ASP.NET's URL Routing
Problem
You want to simplify the URLs on your site using a MapRoute. You also want to leverage these routes to filter the result
sets in the Razor view engine.
 
Search WWH ::




Custom Search