This is sort of a unique requirement where I have contact table that has AccountId as foreign key. Account table has over 11,000 rows so I have to turn server filtering so my Json won't max out. I have a main Contact grid display, and when user clicks to edit that row, custom editor pops up, which has contactId(uneditable), AccountId(DropDownList - but filters when you type 3 letters and gives you the list). However that list is not displaying the initial AccountId/AccountName value (so binding is happening). It seems that this DropDownList not loading initial value was a Kendo bug and they have issued an update. However, it doesn't work in my code. Can anyone please tell me how to do this?

[http://www.telerik.com/forums/kendo-mvc-dropdown-list-settings-initial-value-not-working][1]

My custom Editor Template (_EditContact.cshtml)

@model Humana.Billings.Web.Models.ContactModel
@using Humana.Billings..Web.Helpers;
@using System.Collections;

<div class="editor-label">
    @Html.LabelFor(model => model.ContactId)
</div>
<div class="editor-field">
    @(Html.Kendo().TextBoxFor(model => model.ContactId)
    .Enable(false)
    .HtmlAttributes(new { style = "background-color:silver;color:white" })
    )
</div>

<div id="account-editor-label">
        @Html.LabelFor(model => model.AccountId)
</div>
<div id="account-editor-dropdown">

    @(Html.Kendo().DropDownListFor(model => model.AccountId)
                    .Filter("startswith")
                    .MinLength(3)
                    .DataValueField("AccountId")
                    .DataTextField("Name")
                    .Name("Account")
                    .IgnoreCase(true)
                    .AutoBind(false)
                    .IgnoreCase(true)
                    .Value("Name")
                    .DataSource(source =>
                    {
                        source.Read(read =>
                            read.Action("Editing_Custom", "Contact"));
                        source.ServerFiltering(true);
                    })
)
</div>

Part of my Contact.cshtml (main contact grid)

@model Humana.Billings.Web.Models.ContactModel
@using Humana.Billings.Web.Helpers;
@using System.Collections;

<div>
    @(Html.Kendo().Grid<ContactModel>()
        .Name(Constants.ContactGridId)
        .EnableCustomBinding(true)
        .Sortable()
        .Columns(c =>
        {
            c.Bound(e => e.ContactId).Width(50);
            c.Bound(e => e.AccountId).Hidden();
            c.Bound(e => e.AccountName);
            c.Bound(e => e.AccountNumber);
            c.Bound(e => e.PrimaryContact);
            c.Bound(e => e.ReceivesNotifications);
            c.Bound(e => e.FirstName);
            c.Bound(e => e.LastName);
            c.Bound(e => e.Department).Hidden();
            c.Command(command => { command.Edit(); command.Destroy(); }).Width(155);
        })
        .Events(events =>
        {
            events.Cancel("Helpers.HideNotificationArea");
        })
        .Sortable()
        .Selectable()
        .Editable(editable => editable.Mode(GridEditMode.PopUp)
                    .TemplateName("_EditContact")
                    .Window(w => w.Title("Add / Edit"))
                    )
            })

Here's my ContactController

private List<AccountModel> PopulateAccounts(string text)
{
    try
    {
        var req = new GetAccountRequest();
        var res = new GetAccountResponse();

        req.PageNumber = 0;
        req.PageSize = 0;
        req.SortColumn = string.Empty;
        req.SortDesc = false;
        req.Name = text;

        res = this._accountService.GetAccount(req);

        var accounts = from account in res.Accounts
                   select new AccountModel(account);

        return accounts.ToList();
    }
    catch (Exception ex)
    {
        ex.LogToAFS(CommonTypes.CategoryDetail.Exception);
    }
    return new List<AccountModel>();
}

public ActionResult Editing_Custom(string text)
{
    if ((!string.IsNullOrEmpty(text)) && (text.Length > 3))
    {
        var result = PopulateAccounts(text);
        return Json(result, JsonRequestBehavior.AllowGet);
    }
    return Json(new List<AccountModel>(), JsonRequestBehavior.AllowGet);    
}

Related posts

Recent Viewed