forum

Home / DeveloperSection / Forums / I am using mvc4 in that i am using grid view in that it is not searching a data

I am using mvc4 in that i am using grid view in that it is not searching a data

anand kumar 1500 05-Aug-2016
this is my controller

 [HttpPost]
        public ActionResult SearchResult(string SearchKey)
        {
            ProductDetailModel obj = new ProductDetailModel();
            obj.ProductList = ProductId(SearchKey);
            string strTable = "";
            strTable = strTable + "< table>< thead>< tr>< th>Id</ th >< th >Product_Name</ th >";
            strTable = strTable + "< th >Product_Detail</ th >< th >price< / th></ tr></ thead>< tbody>";

            foreach (var item in obj.ProductList)
            {
                strTable = strTable + "< tr >";
                strTable = strTable + "< td >" + item.Id + "</ td >";
                strTable = strTable + "< td >" + item.Product_Name + "</ td >";
                strTable = strTable + "< td >" + item.Product_Detail + "</ td >";
                strTable = strTable + "< td>" + item.price + "</ td >";
                strTable = strTable + "</ tr >";
            }
            strTable = strTable + "</ tbody ></ table >";
            return View(strTable);
        }

        
        [HttpGet]
        public List<product> ProductId(string SearchKey)
        {
            List<product> objProduct = new List<product>();
            gridEntities objDemoEntities = new gridEntities();
            var productItem = from data in objDemoEntities.grids
                              where SearchKey == "" ? true : data.Product_Name.Contains(SearchKey)
                              select data;
        
                foreach (var item in productItem)
                {
                    objProduct.Add(new product
                    {
                        Id =(int) item.Id,
                        Product_Name = item.Product_Name,
                        Product_Detail = item.Product_Details,
                        price = (int)item.price
                    });
                }
                return objProduct;
            }

this is my Index.view

@model grid01.Models.ProductDetailModel
@{
    ViewBag.Title = "SearchResult";
}
<script src="../../js/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
    function AjaxSearch() {
        var searchUrl = "Home/SearchResult/?SearchKey=" + $("#SearchKey").attr('value');
        $("#btnSearch").attr({ 'value': 'Please wait..' });
        $.ajax({
            url: "SearchUrl",
            type: "POST",
            success: function (data) {
                $("#divData").html(data);
                $("#btnSearch").attr({ 'value': 'Search' });
            }
        });
    }
</script>
@using (@Html.BeginForm("Index", "Home"))
{
    <table border="0">
        <tr>
            <td align="right">
                Search: @Html.TextBox("SearchKey", "", new { })
            </td>
            <td align="left">
                <input type="button" value="Search" onclick="javascript:AjaxSearch();" id="btnSearch" />
            </td>
            <td>
                <p>
                    @Html.ActionLink("Insert", "add")
                </p>
            </td>
        </tr>
    </table>
             <div id="msgwait"></div>
    <div id="divData">
        <div style="text-align:center;display:@Model.ProductList">
            @{
    var grid = new WebGrid(Model.ProductList, canSort: false);




    @grid.GetHtml(
    tableStyle: "Grid-table",
     headerStyle: "Grid-Header",
     alternatingRowStyle: "Grid-alt",
     selectedRowStyle: "Grid-selected",
     rowStyle: "Grid-rowstyle",
     footerStyle: "Grid-Footer",

    columns:
                grid.Columns
                (
                        grid.Column("Id", "Id"),
                        grid.Column("Product_Name", "Product_Name"),
                        grid.Column("Product_Detail", "Product_Detail"),
                        grid.Column("price", "price")
        ), mode: WebGridPagerModes.All);

            }

            
        </div>
        </div>
    foreach (var item in Model.ProductList)
    {
                                <tr>
                                    <td>
                                        @Html.ActionLink("Edit", "Edit", new { id = item.Id })
                                    </td>
                                </tr>
                                }
                                

   
}



Updated on 05-Aug-2016

Can you answer this question?


Answer

0 Answers

Liked By