This is a migrated thread and some comments may be shown as answers.

Cannot load data from database into grid on button click

1 Answer 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dev
Top achievements
Rank 1
dev asked on 22 Mar 2014, 01:20 AM
This is a Search page. where I enter the name and dept name and hit search, This should load my grid with all matching data from the backend database.
I cannot get this to work in mvc4 vs 2012. I am new to mvc and I really need help
Help!!!!!!!

Index.cshtml
@model MvcMaster.Models.SearchModel
@{
ViewBag.Title = "Search";
}
<h2>Search Policy</h2>
<section id="searchForm">
<script type="text/javascript">
//$(function () {
// $('#SearchBtn').click(function (e) {
$(document).ready(function () {
$('#SearchBtn').click(function () {
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '@(Url.Action("BtnClick", "Search"))',
success: function (result) {
var grid = $("#MyGrid").data("kendoGrid"); 
grid.dataSource.data(result);
grid.refresh();
}
});
return false;
})
})
</script>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
<legend>Search for a Policy</legend>
<ol>
<li>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</li>
<li>
@Html.LabelFor(m => m.DeptName)
@Html.TextBoxFor(m => m.DeptName)
@Html.ValidationMessageFor(m => m.DeptName)
</li> 
<input type="submit" value="Search" id="SearchBtn"/>
<div id="searchGrid">
@(Html.Kendo().Grid<MvcMaster.Models.SearchModel>()
.Name("PolicyGrid")
.AutoBind(false)
.DataSource(dataSource => dataSource.Ajax()
.Read(read => read.Action("BtnClick", "Search"))
.PageSize(20)
)
.Columns(columns =>
{
columns.Bound(c => c.Id);
columns.Bound(c => c.Name).Title("Name");
columns.Bound(c => c.EMpNumber).Title("Id");
columns.Bound(c => c.JoinDate).Title("Effective Date");
columns.Bound(c => c.DeptName).Title("Department");
})
.Pageable(page => page.Enabled(true).PageSizes(new[] {10, 20, 30, 40}))
.Sortable(sorting => sorting.SortMode(GridSortMode.SingleColumn))
.Scrollable()
 )))
</div>
</fieldset>
}
</section>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")

}

Model.cs
public class SearchModel
{
public string Id { get; set; }
public string Name { get; set; }
public string EmpNumber { get; set; }
public DateTime JoinDate { get; set; }
public stringDeptName { get; set; }
 
}

Controller
// POST: /Search/
[HttpPost]
 
public ActionResult BtnClick(string name, string DptName, [DataSourceRequest]DataSourceRequest request)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(connectionstring))
{
SqlCommand cmd = new SqlCommand("sp_mysproc", con);
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.AddWithValue("@searchEmpName", name);
cmd.Parameters.AddWithValue("@searchDeptName", DptName); 
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);

DataSourceResult result = dt.ToDataSourceResult(request);
 return Json(result, JsonRequestBehavior.AllowGet);
}
}

1 Answer, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 25 Mar 2014, 11:01 AM
Hi,

There are several ways to achieve the desired behavior and which one you would choose depends entirely on you and the exact setup that you have. For example you can setup the Grid to send additional data through the Read action which to filter the data on the server side:

<input type="button" value="Search" id="SearchBtn" onclick="filterGrid()" />
<script>
    function filterGrid() {
        $("#PolicyGrid").data("kendoGrid").dataSource.read();
    }
 
    function sendAddtionalData() {
        return {
            name: $("#Name").val(),
            DptName: $("#DptName").val()
        };
    }
</script>
@(Html.Kendo().Grid<MvcMaster.Models.SearchModel>()
    .Name("PolicyGrid")
    .AutoBind(false)
    .DataSource(dataSource => dataSource.Ajax()
        .Read(read => read.Action("BtnClick", "Search").Data("sendAddtionalData"))
    .PageSize(20)
    )
    .Columns(columns =>
    {
    columns.Bound(c => c.Id);
    columns.Bound(c => c.Name).Title("Name");
    columns.Bound(c => c.EMpNumber).Title("Id");
    columns.Bound(c => c.JoinDate).Title("Effective Date");
    columns.Bound(c => c.DeptName).Title("Department");
    })
    .Pageable(page => page.Enabled(true).PageSizes(new[] {10, 20, 30, 40}))
    .Sortable(sorting => sorting.SortMode(GridSortMode.SingleColumn))
    .Scrollable()
)

Regards,
Vladimir Iliev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
dev
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Share this question
or