javascript error when model for DropDownListFor is null

2 posts, 1 answers
  1. Alex
    Alex avatar
    20 posts
    Member since:
    May 2015

    Posted 07 Apr Link to this post

     

    I have a grid that has a column bound to it:

    columns.Bound(p => p.FailureStrategyLookup).ClientTemplate("#=FailureStrategyLookup.FailureStrategy#").Width(100);

     

    If the FailureStrategyLookup is NOT null, everything works fine. But when it IS NULL it can't bind the the object and I get an error saying "FailureStrategy" can't be found for a null value (or something to that affect). How can I get thsi working for a COMPLEX type when it's null?

     

    public class FailureStrategyLookupViewModel
    {
        [Required]
        public int FailureStrategyID { get; set; }
     
        [Required]
        [DisplayName("Maintenance Strategy")]
        public string FailureStrategy { get; set; }
    }

    ssdsdf

     

    @using eRPortalDashboard.Models;
    @using Kendo.Mvc.UI;
     
    @model FailureStrategyLookupViewModel
     
    @(Html.Kendo().DropDownListFor(m => m)
        .DataValueField("FailureStrategyID")
        .DataTextField("FailureStrategy")
        .BindTo((System.Collections.IEnumerable)TempData["FailureStrategyLookups"])
    )

    sadasd
  2. Answer
    Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 11 Apr Link to this post

    Hello Alex,

    From the provided information it seems that the source if the issue is the column template that you are using. In order to support null values you should access the nested fields only if the parent field is not null. Please check the updated code below:

    .ClientTemplate("#=FailureStrategyLookup ? FailureStrategyLookup.FailureStrategy : 'no value'#")

    Regards,
    Vladimir Iliev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top