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);
}
}
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);
}
}