|
[index.cshtml]
<button id="btn" onclick="update()">Update Datasource</button>
<div>
@(Html.EJ().Grid<object>("FlatGrid")
.Datasource((IEnumerable<object>)ViewBag.datasource)
.AllowPaging()
.AllowSorting()
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
col.Field("CustomerID").HeaderText("CustomerID").Width(90).Add();
col.Field("EmployeeID").HeaderText("EmployeeID").TextAlign(TextAlign.Right).Width(75).Add();
col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add();
})
)
</div>
<script type="text/javascript">
function update() {
$.ajax({
type: "POST",
url: "/Home/Data",
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
$("#FlatGrid").ejGrid("dataSource", result);
},
error: function (args) {
alert('error occurred');
}
});
}
</script>
[Controller.cs]
namespace EJGrid.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.datasource = OrderRepository.GetAllRecords().Take(30).ToList();
return View();
}
public ActionResult Data()
{
var datasource = OrderRepository.GetAllRecords().ToList();
return Json(datasource,JsonRequestBehavior.AllowGet);
}
}
} |