Hello,
I think I may have a found a bug with how a Grid is displaying data after I've added a record to the table it's bound to. The meat of the problem is in the javascript I'm using to call dataSource.read() after I've added the record. My first attempt looked as follows:
<script> function NewDefectFeatureSubmitButtonClick(e) { var defectFeatureGrid = $("#DefectFeatureGrid").data("kendoGrid"); var newDefectFeatureTextBox = $("#NewDefectFeatureNameTextBox"); var newDefectFeatureName = newDefectFeatureTextBox.val(); var dummyUrl = '@Url.Action("AddDefectFeature", "DefectFeature", new { name = "--V1--" })'; var url = dummyUrl.replace("--V1--", newDefectFeatureName); $.ajax( { dataType: "json", type: "POST", url: url }); Sleep(1000); // needed to allow the ajax call above to complete before re-reading the datasource defectFeatureGrid.dataSource.read(); newDefectFeatureTextBox.val(""); }</script>Notice how I'm passing the parameter via the URL...then calling dataSource.read(). This works just fine and the grid updates with the new record...however I felt this looked "hacky" so I wanted to update it to use the data object in the ajax call....which looks like the following:
<script> function NewDefectFeatureSubmitButtonClick(e) { var defectFeatureGrid = $("#DefectFeatureGrid").data("kendoGrid"); var newDefectFeatureTextBox = $("#NewDefectFeatureNameTextBox"); var newDefectFeatureName = newDefectFeatureTextBox.val(); var url = '@Url.Action("AddDefectFeature", "DefectFeature")'; $.ajax( { data: { name: newDefectFeatureName }, dataType: "json", type: "POST", url: url }); Sleep(1000); // needed to allow the ajax call above to complete before re-reading the datasource defectFeatureGrid.dataSource.read(); newDefectFeatureTextBox.val(""); }</script>notice here how I'm passing the parameter into the data object for the ajax call, then the same as before calling dataSource.read(). The difference being is in this case, the grid does NOT add the new row.
When I look at the database, the record gets added regardless to how the parameter is passed into the ajax call.
When I look at the fiddler traces from the read calls:
- parameter passed via URL - The fiddler trace includes the new record
- parameter passed via data - The fiddler trace does NOT include the new record even though the database shows it's there.
I've no idea why this is happening, but it repro's 100% of the time. Is this a bug? If not can someone explain to me why it's not? I have another view where I don't have a choice and I have to pass the parameters via the data object...so I need to get it solved eventually or I'm going to be blocked.
Here's the rest of my source files for context:
Controller:
namespace TPOReports.Controllers{ using System.Web.Configuration; using System.Web.Mvc; using Kendo.Mvc.UI; using Kendo.Mvc.Extensions; using TPOReporting; /// <summary> /// controller class for dealing with DefectFeature related actions /// </summary> public class DefectFeatureController : Controller { /// <summary> /// returns the default view for the this controller /// </summary> /// <returns>DefectFeatureView</returns> [HttpGet] public ActionResult DefectFeatureView() { return View(); } /// <summary> /// adds a new DefectFeature into the database /// </summary> /// <param name="name">name of DefectFeature to add</param> /// <returns>DefectFeatureView</returns> [HttpPost] public ActionResult AddDefectFeature(string name) { DataMaintenance dataMaintenance = new DataMaintenance(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); dataMaintenance.AddDefectFeature(name); return RedirectToAction("DefectFeatureView"); } /// <summary> /// retrieves all DefectFeature objects from the database /// </summary> /// <returns>a collection of DefectFeature objects as json</returns> [HttpPost] public ActionResult GetDefectFeatures([DataSourceRequest] DataSourceRequest request) { DataMaintenance dataMaintenance = new DataMaintenance(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); var defectFeaturesPayload = dataMaintenance.GetDefectFeatures().Payload; DataSourceResult result = defectFeaturesPayload.ToDataSourceResult(request); return Json(result); } }}View with working javascript block:
@using TPOReporting@{ ViewBag.Title = "Defect Feature Maintenance"; Layout = null;}<script> function NewDefectFeatureSubmitButtonClick(e) { var defectFeatureGrid = $("#DefectFeatureGrid").data("kendoGrid"); var newDefectFeatureTextBox = $("#NewDefectFeatureNameTextBox"); var newDefectFeatureName = newDefectFeatureTextBox.val(); var dummyUrl = '@Url.Action("AddDefectFeature", "DefectFeature", new { name = "--V1--" })'; var url = dummyUrl.replace("--V1--", newDefectFeatureName); $.ajax( { dataType: "json", type: "POST", url: url }); Sleep(1000); // needed to allow the ajax call above to complete before re-reading the datasource defectFeatureGrid.dataSource.read(); newDefectFeatureTextBox.val(""); }</script><div> <h2>Defect Features</h2> <hr size="3" /></div><div> <table> <tr> <td> <label>New Defect Feature Name:</label> </td> <td> @(Html.Kendo().TextBox() .Name("NewDefectFeatureNameTextBox")) </td> <td> @(Html.Kendo().Button() .Name("NewDefectFeatureSubmitButton") .Content("Submit") .Events(e => e.Click("NewDefectFeatureSubmitButtonClick"))) </td> </tr> </table></div><div> @(Html.Kendo().Grid<DefectFeature>() .Columns(c => { c.Bound(df => df.Name) .Title("Name"); c.Bound(df => df.LastUpdate) .Title("Last Update"); }) .DataSource(ds => ds .Ajax() .Read(r => r.Action("GetDefectFeatures", "DefectFeature"))) .HtmlAttributes(new { style = "height:300px;" }) .Name("DefectFeatureGrid") .Scrollable() .Selectable() .Sortable())</div>