This is a migrated thread and some comments may be shown as answers.

ArgumentNullException with ForeignKey in EditorTemplates

5 Answers 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sébastien
Top achievements
Rank 1
Sébastien asked on 27 Jun 2014, 01:15 PM
I use that:
    @(Html.Kendo().Grid<Lorem.Core.DataModels.Contacts.DMConEmail>()
    .Name("ContactEmails")
    .Columns(columns =>
    {
        columns.ForeignKey(p => p.Type, Html.GetEnumList(typeof(Lorem.Core.DataModels.Contacts.DMConEmailType)));
        columns.Bound(p => p.Email);
        columns.Command(command => { command.Edit(); command.Destroy(); });
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .DataSource(datasource => datasource.Ajax()
        .Model(model => model.Id(p => p.IdEmail))
        .Create(update => update.Action("EmailInsert", "Contact", new { idContact = Model.IdContact }))
        .Read(read => read.Action("EmailGetList", "Contact", new { idContact = Model.IdContact }))
        .Update(update => update.Action("EmailUpdate", "Contact"))
        .Destroy(update => update.Action("EmailDelete", "Contact"))
        ))

And I have a column that use an Enum to choose the email type. When I use that in a view, all is working fine. When I use it in an EditorTemplates I have this exception:
Ligne 1 : @model object
Ligne 2 :
Ligne 3 : @(
Ligne 4 : Html.Kendo().DropDownListFor(m => m)
Ligne 5 : .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])

[ArgumentNullException: La valeur ne peut pas être null.
Nom du paramètre : source]
System.Linq.Enumerable.Select(IEnumerable`1 source, Func`2 selector) +4090943
Kendo.Mvc.UI.Fluent.DropDownListBuilder.BindTo(IEnumerable`1 dataSource) +88
ASP._Page_Views_Shared_EditorTemplates_GridForeignKey_cshtml.Execute() in c:\DCM\SVNPortailDCM\trunk\Softwares\Lorem\MVC\Views\Shared\EditorTemplates\GridForeignKey.cshtml:3
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +198
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +90
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235

5 Answers, 1 is accepted

Sort by
0
Sébastien
Top achievements
Rank 1
answered on 27 Jun 2014, 01:17 PM
The GetEnumList function:

        public static SelectList GetEnumList(this HtmlHelper htmlHelper, Type type)
        {
            List<SelectListItem> result = new List<SelectListItem>();
            foreach(Enum value in (Enum.GetValues(type))) {
                result.Add(new SelectListItem() { Value = Convert.ToInt32(value).ToString(), Text = AttributeUtil.GetDisplayName(value) });
            }
            return new SelectList(result, "Value", "Text");
        }
0
Sébastien
Top achievements
Rank 1
answered on 27 Jun 2014, 04:49 PM
The exception don't seem to come from EditorTemplates, but come from a model property containing another model class used by the child EditorTemplate. The class hierarchy don't seem to work like another Microsoft MVC Controls.
0
Daniel
Telerik team
answered on 01 Jul 2014, 01:43 PM
Hello,

The exception would be thrown by the dropdownlist if there is a prefix for the editor template because the ViewData key will not be correct and null will be passed to the BindTo method. You should clear the prefix before rendering the grid and restore it after if needed:
@{
    var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;
}
 
@(Html.Kendo().Grid<Lorem.Core.DataModels.Contacts.DMConEmail>()
    .Name("ContactEmails")
    ...
)
 
@{
    ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}

in order to avoid the exception and avoid the editors to be generated with a name that is incorrect in the context of the grid editing:


Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sébastien
Top achievements
Rank 1
answered on 02 Jul 2014, 01:48 PM
If I clear the prefix, the ViewModel was all null, so I can't clear the prefix.
0
Daniel
Telerik team
answered on 04 Jul 2014, 07:51 AM
Hello again,

Could you provide a runnable sample that demonstrates the issue when clearing the prefix?

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Sébastien
Top achievements
Rank 1
Answers by
Sébastien
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or