Note that there are some explanatory texts on larger screens.

plurals
  1. POForeignKey column does not select value in Kendo-Grid
    text
    copied!<p>I have a grid with some columns where one of these columns is a foreignKey column.</p> <p>I want to show all possible values for that column in a combobox. <code>ViewData["States"]</code> is an <code>IList&lt;State&gt;</code> where State has an <code>Id</code> field and a <code>Name</code> field.</p> <p>To achieve that, I modified the template "GridForeignKey.cshtml" like the following:</p> <pre><code>@model object @( Html.Kendo().ComboBoxFor(m =&gt; m) .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"]).Filter(FilterType.Contains).Placeholder("Select...") ) </code></pre> <p>My View looks like this:</p> <pre><code>&lt;div class="contentwrapper"&gt; @{ ViewBag.Title = "Home Page"; Layout = "~/Views/Shared/_Layout.cshtml"; } @( Html.Kendo().Grid&lt;CustomerModel&gt;() .Name("Grid") .Columns(columns =&gt; { columns.Bound(p =&gt; p.Name); columns.ForeignKey(p =&gt; p.StateId, (IEnumerable)ViewData["States"], "Id", "Name"); columns.Bound(p =&gt; p.StreetAddress).Width(140); columns.Bound(p =&gt; p.Zip).EditorTemplateName("Integer"); columns.Command(command =&gt; { command.Edit(); command.Destroy(); });//edit and delete buttons }) .ToolBar(toolbar =&gt; toolbar.Create())//add button .Editable(editable =&gt; editable.Mode(GridEditMode.InLine))//inline edit .Pageable() .Sortable() .Scrollable() .Filterable() .DataSource(dataSource =&gt; dataSource .Ajax() .Events(events =&gt; events.Error("error_handler")) .Model(model =&gt; { model.Id(c =&gt; c.CustomerId); model.Field(c =&gt; c.CustomerId).Editable(false); model.Field(c =&gt; c.Zip).DefaultValue(4444); } )//id of customer .Create(update =&gt; update.Action("EditingInline_Create", "Customer")) .Read(read =&gt; read.Action("EditingInline_Read", "Customer")) .Update(update =&gt; update.Action("EditingInline_Update", "Customer")) .Destroy(update =&gt; update.Action("EditingInline_Destroy", "Customer")) ) ) &lt;/div&gt; &lt;script type="text/javascript"&gt; function error_handler(e) { if (e.errors) { var message = "Errors:\n"; $.e`enter code here`ach(e.errors, function (key, value) { if ('errors' in value) { $.each(value.errors, function () { message += this + "\n"; }); } }); alert(message);//show error } } &lt;/script&gt; </code></pre> <p>Now my problems:</p> <ol> <li>My Table does not show the selected value for "State". </li> <li>When I edit a row, the combobox shows up and has all the desired values in it, but the value is not chosen. Instead it always shows the placeholder-text.</li> <li>Before I had a complex object bound to my grid which had a field which was a complex type itself (<code>State</code> which contained the <code>Id</code> and <code>Name</code> attributes) but somehow kendo wouldnt let me bind it like <code>p =&gt; p.State.Id</code>. That's why I have flattened my model, and I now use the field <code>StateId</code> instead. Is it even possible to use a cascaded complex type like this?</li> </ol>
 

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