Note that there are some explanatory texts on larger screens.

plurals
  1. POKendoUI Grid - ForeignKey column not working in PopUp edit mode
    text
    copied!<p>I've searched for all over the place (understatement) for a solution to my case to no avail until now. First, I'll explain my scenario:</p> <ul> <li>I have an OpenAccess Model exposed as a WCF Data Service (oData v3);</li> <li>I have an Kendo MVC Application;</li> <li>I have a View with a grid, set for PopUp editing, AJAX Bound;</li> </ul> <p>Before posting some code, let me explain my issue/difficulty. I have an entity with these properties:</p> <ul> <li>TextoID</li> <li>Titulo;</li> <li>Corpo;</li> <li>TipoTextoID;</li> <li>TipoTexto;</li> </ul> <p>There is a ForeignKey column set to the TipoTextoID property which get's correctly populated either in in-line or pop-up mode. But when it comes to changing data, it only works in-line mode. This is my issue I need it to work in a popup, since the "Corpo" property is bound to a KEndoUI Editor.</p> <p>When in the popup, it does not show the correct value on the dropdown neither changes it when we select it.</p> <p>Honestly I'm feeling stupid. I tried almost every sample, post, article I could find to no avail and I'm clueless.</p> <p>I hope someone can help me on this. Thanks in advance to all!</p> <p>So, here's the code. The view:</p> <pre><code> @model IEnumerable&lt;KendoMVC.CostSimulatorService.Texto&gt; @{ ViewBag.Title = "Textos"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Textos&lt;/h2&gt; @(Html.Kendo().Grid(Model) // Bind the grid to the Model property of the view .Name("Grid") .Columns(columns =&gt; { columns.Bound(p =&gt; p.Titulo); //Create a column bound to the "ProductID" property //columns.Bound(p =&gt; p.IsPrivado).ClientTemplate("&lt;input type='checkbox' #= IsPrivado ? checked='checked': '' # class='chkbx' /&gt;"); //Create a column bound to the "ProductName" property columns.Template(@&lt;text&gt;&lt;/text&gt;).ClientTemplate("&lt;input type='checkbox' #= IsPrivado ? checked='checked': '' # class='chkbx' /&gt;"); //Create a column bound to the "ProductName" property //columns.Bound(p =&gt; p.TiposTexto); columns.ForeignKey(p =&gt; p.TipoTextoID, (System.Collections.IEnumerable)ViewData["TiposTexto"], "TipoTextoID", "Designacao") .Title("Tipo de texto").Width(150); columns.Command(command =&gt; { command.Edit(); command.Destroy(); }).Width(200); }) .ToolBar(commands =&gt; commands.Create()) .Editable(editable =&gt; editable.Mode(GridEditMode.PopUp).TemplateName("Texto")) .DataSource(dataSource =&gt; dataSource .Ajax() //specify server type .Model(model =&gt; { model.Id(texto =&gt; texto.TextoID); // Specify the property which is the unique identifier of the model model.Field(texto =&gt; texto.TextoID).Editable(false); // Make the ProductID property not editable }) .Create(create =&gt; create.Action("CreateTexto", "BackOffice")) .Read(read =&gt; read.Action("ReadTextos", "BackOffice")) .Update(update =&gt; update.Action("UpdateTexto", "BackOffice")) .Destroy(destroy =&gt; destroy.Action("DestroyTexto", "BackOffice"))) .Pageable() // Enable paging .Sortable() // Enable sorting .Selectable() .Filterable() .Scrollable() ) &lt;script type="text/javascript"&gt; $(document).ready(function() { $("form.k-edit-form").kendoValidator(); }); &lt;/script&gt; </code></pre> <p>Next, then template:</p> <pre><code>@using System.Web.Mvc.Html; @model KendoMVC.CostSimulatorService.Texto Introduza o conteúdo que deseja @Html.HiddenFor(model =&gt; model.TextoID) &lt;div id="divWrapper" style="width:99%; float:left;"&gt; &lt;div&gt; @Html.LabelFor(model =&gt; model.Titulo) &lt;/div&gt; &lt;div&gt; @Html.EditorFor(model =&gt; model.Titulo) @Html.ValidationMessageFor(model =&gt; model.Titulo) &lt;/div&gt; &lt;div&gt; @Html.LabelFor(model =&gt; model.Corpo) &lt;/div&gt; &lt;div&gt; @(Html.Kendo().EditorFor(model =&gt; model.Corpo)) @Html.ValidationMessageFor(model =&gt; model.Corpo) &lt;/div&gt; &lt;div&gt; @Html.LabelFor(model =&gt; model.TipoTextoID) &lt;/div&gt; &lt;div&gt; @*@(Html.Kendo().DropDownListFor(model =&gt; model.TiposTexto)) @Html.ValidationMessageFor(model =&gt; model.TiposTexto)*@ @(Html.Kendo().DropDownListFor(m =&gt; m.TipoTextoID) .Name("TiposTexto") .DataTextField("Designacao") .DataValueField("TipoTextoID") .BindTo((System.Collections.IEnumerable) ViewData["TiposTexto"])) &lt;/div&gt; &lt;/div&gt; </code></pre> <p>The controller:</p> <pre><code>using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using Kendo.Mvc.Extensions; using Kendo.Mvc.UI; using KendoMVC.CostSimulatorService; namespace KendoMVC.Controllers { public partial class BackOfficeController : Controller { #region CRUD #region ReadTextos public ActionResult ReadTextos([DataSourceRequest]DataSourceRequest request) { CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/")); IQueryable&lt;Texto&gt; textos = modelo.Textos; DataSourceResult resultado = textos.ToDataSourceResult(request); ViewData["Textos"] = textos; return Json(resultado, JsonRequestBehavior.AllowGet); } #endregion #region CreateTexto public ActionResult CreateTexto([DataSourceRequest]DataSourceRequest request, Texto texto) { if (ModelState.IsValid) { CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/")); // Create a new Product entity and set its properties from the posted ProductViewModel Texto entity = new Texto { TextoID = texto.TextoID, Titulo = texto.Titulo, Corpo = texto.Corpo, IsPrivado = texto.IsPrivado, TipoTextoID = texto.TipoTextoID, TiposTexto = texto.TiposTexto }; modelo.AddToTextos(entity); // Insert the entity in the database modelo.SaveChanges(); // Get the ProductID generated by the database texto.TextoID = entity.TextoID; } // Return the inserted product. The grid needs the generated ProductID. Also return any validation errors. return Json(new[] { texto }.ToDataSourceResult(request, ModelState)); } #endregion #region UpdateTexto public ActionResult UpdateTexto([DataSourceRequest]DataSourceRequest request, Texto texto) { if (ModelState.IsValid) { CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/")); // Create a new Product entity and set its properties from the posted ProductViewModel var entity = new Texto { TextoID = texto.TextoID, Titulo = texto.Titulo, Corpo = texto.Corpo, IsPrivado = texto.IsPrivado, TipoTextoID = texto.TipoTextoID, TiposTexto = texto.TiposTexto }; // Attach the entity modelo.AttachTo("Textos", entity); modelo.UpdateObject(entity); // Update the entity in the database modelo.SaveChanges(); } // Return the updated product. Also return any validation errors. return Json(new[] { texto }.ToDataSourceResult(request, ModelState)); } #endregion #region DestroyTexto public ActionResult DestroyTexto([DataSourceRequest]DataSourceRequest request, Texto texto) { if (ModelState.IsValid) { CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/")); // Create a new Product entity and set its properties from the posted ProductViewModel var entity = new Texto { TextoID = texto.TextoID //Titulo = texto.Titulo, //Corpo = texto.Corpo, //IsPrivado = texto.IsPrivado, //TipoTextoID = texto.TipoTextoID }; // Attach the entity modelo.AttachTo("Textos", entity); // Delete the entity modelo.DeleteObject(entity); // Delete the entity in the database modelo.SaveChanges(); } // Return the removed product. Also return any validation errors. return Json(new[] { texto }.ToDataSourceResult(request, ModelState)); } #endregion #endregion } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload