This article will show you how you can perform search
functionality and display the search result in mvc webgrid in
asp.net mvc using
c#.net.
Some of my previous articles are as follows: Comment
System OR Form and Display In GridView Using C# In Asp.Net, Hyperlink
Adding In MVC WebGrid in Asp.Net MVC Using C#.Net, Bind
WebGrid With Entity Framework in Asp.Net MVC Using C#.Net, Asp.Net
MVC Export Data to Excel File Of WebGrid Using C#.Net, Responsive
Grid Design Example Using Css in Asp.Net MVC,HTML, Bind
jQuery DatePicker Calendar In MVC WebGrid and Retrive Value Using Asp.net MVC,
C#.Net, Bind
DropDownList In MVC WebGrid and Retrive Value Using Asp.net MVC, C#.Net, Add
Textbox In WebGrid and Access TextBox Value In Controller In Asp.net MVC Using
C#.Net.
So for this article first we will create an asp.net mvc
application and add the now add the entity table.
Now add a new model class file and add the below code.
Now add a new model class file and add the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class ProductDetailModel
{
public
List<Product>
ProductList { get; set;
}
}
public class Product
{
public
int ID { get;
set; }
public
string ProductName { get;
set; }
public
string ProductDetail { get; set; }
public
int CurrentStock { get;
set; }
}
}
|
Now add a controller class file and the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
namespace MvcApplication1.Controllers
{
public class HomeController
: Controller
{
//
// GET:
/Home/
[HttpGet]
public
ActionResult Index()
{
ProductDetailModel
objproductdetailmodel = new ProductDetailModel();
objproductdetailmodel.ProductList
= ProductData("");
return
View(objproductdetailmodel);
}
[HttpPost]
public
ActionResult Index(string SearchKey)
{
ProductDetailModel
objproductdetailmodel = new ProductDetailModel();
objproductdetailmodel.ProductList
= ProductData(SearchKey);
return
View(objproductdetailmodel);
}
///
/// This finction is used for providing the data.
/// we will bind this data
/// to diaply in grid format.
/// You can get data from data base and
/// thee put into collection
///
public List<Product> ProductData(string SearchKey)
{
List<Product> objProduct = new List<Product>();
DemoEntities objDemoEntities = new DemoEntities();
var productItem = from data in objDemoEntities.ProductMasters
where SearchKey == "" ? true : data.ProductDetail.Contains(SearchKey)
select data;
foreach (var item in productItem)
{
objProduct.Add(new Product
{
ID = item.Id,
ProductName = item.ProductName,
ProductDetail = item.ProductDetail,
CurrentStock = (int)item.CurrentStock
});
}
return objProduct;
}
}
}
|
In above code I have added the get and post both the
methods. In this get method will bind the detail on page load and post method
will display the search result.
Now create the view and add the below code.
@model MvcApplication1.Models.ProductDetailModel
@{
ViewBag.Title = "Search
and Display In MVC WebGrid in Asp.Net MVC Using C#.Net";
}
<style>
table, td, th
{
border:
1px solid green;
border-collapse:
collapse;
}
th
{
border:
1px solid black;
background-color:
green;
color:
white;
}
</style>
<script language="javascript">
function
AlertMessage() {
alert("This
is a alert message");
}
</script>
@using (@Html.BeginForm("Index", "Home"))
{
<table width="97%" border="0">
<tr>
<td align="right">
Search by detail: @Html.TextBox("SearchKey", "")
</td>
<td align="left">
<input type="submit" value="Search" />
</td>
</tr>
</table>
var
grid = new WebGrid(Model.ProductList,
canSort: false);
<div>
@grid.GetHtml(columns:
grid.Columns
(
grid.Column("ID", "ID"),
grid.Column("ProductName", "Product Name"),
grid.Column("ProductDetail", "Product Detail"),
grid.Column("CurrentStock", "Current Stock")
), mode: WebGridPagerModes.Numeric)
</div>
}
|
Here the name of the search textbox is same as the parameter passed in the post index method.
Now we have down run the application the check the output.
In this search will be performed on the bases of product detail.
In this search will be performed on the bases of product detail.
good post.
ReplyDeleteThanks for your valuable comment
Deletegood post.
ReplyDeleteThanks for your valuable comment
DeleteHow to add form data on UI to table on UI and then pass it to Controller??
ReplyDeleteHi check the link
Deletehttp://www.aspdotnet-pools.com/2014/07/how-to-bind-data-to-webgrid-in-aspnet.html
hi, I am working on creating an app with search and want to render the results into the same view. when i use your approach my model object is empty and the webgrid throws a system.nullexception. Can you please help me in what needs to be done?
DeleteThanks
hi..It is a good post .but i think you miss the post the code of DemoEntities class.please let me know what is the DemoEntities class code.
ReplyDeleteHi It's the entity class file. here entity(.edmx) file name is DemoEntities
Delete