I have a little problems with refreshing data in asp.net mvc grid which is inside a partial view.
This is my grid in partial view:
<div class="row">
<div class="col-md-12">
@(Html.EJ().Grid<recognitions>("gridRec")
.Datasource((IEnumerable<recognitions>)ViewBag.dsRec)
.IsResponsive()
.AllowSelection(false)
.EnableAltRow(true)
.Columns(col =>
{
col.Field("source_id").HeaderText("source_id").TextAlign(TextAlign.Right).Width(75).Add();
col.Field("plate_text").HeaderText("plate_text").TextAlign(TextAlign.Right).Width(75).Add();
col.Field("country_assumed").HeaderText("country_assumed").Width(80).Add();
col.Field("confidence_level").HeaderText("confidence_level").TextAlign(TextAlign.Right).Priority(2).Width(75).Add();
col.Field("complete_image_url").HeaderText("complete_image_url").TextAlign(TextAlign.Right).Width(75).Priority(3).Format("{0:C}").Add();
col.Field("date").HeaderText("date").TextAlign(TextAlign.Right).Width(75).Format("{0:dd.MM.yyyy. hh:mm:ss}").Add();
col.HeaderText("plate_image").Template("<img src='data:image/jpeg;base64,{{:plate_image_url}}' />").TextAlign(TextAlign.Center).Width(100).Add();
})
)
</div>
</div>
This is in my controller:
public ActionResult Rec() // This is for oppening partial view from main view
{
var lstRec = new ANPREntities().recognitions.OrderByDescending(r => r.date).ToList();
foreach (var item in lstRec)
{
item.plate_image_url = Convert.ToBase64String(System.IO.File.ReadAllBytes(item.plate_image_url.ToString()));
}
ViewBag.dsRec = lstRec;
lstRec = null;
return PartialView("_IndexRec");
}
[HttpGet]
public JsonResult GetRec() // This is for get just the data for the grid
{
var lstRec = new ANPREntities().recognitions.OrderByDescending(r => r.date).ToList();
foreach (var item in lstRec)
{
item.plate_image_url = Convert.ToBase64String(System.IO.File.ReadAllBytes(item.plate_image_url.ToString()));
}
return Json(lstRec, JsonRequestBehavior.AllowGet);
}
JS in main view for updating the grid is this:
var obj = $("#gridRec").data("ejGrid");
$.ajax({
url: '@Url.Action("GetRec", "Home")',
type: "get",
}).done(function (data) {
obj.dataSource(data);
}).fail(function () {
alert('error');
});
So, why is refreshing the grid works only once? :(
Thanks!