Mvc.CascadeDropDown icon indicating copy to clipboard operation
Mvc.CascadeDropDown copied to clipboard

Can't get Overload for CascadingDropDownList() to work

Open smann004 opened this issue 8 years ago • 4 comments

Nice work with this package. It could save many hours of coding if I could overcome one last hurdle. I have been able to get CascadingDropDownListFor() to work however the way I display and bind my dropdownlists follows the pattern @Html.DropDownList("ProjectTypeID", Model.ProjectTypeList).

This VB.NET Razor code works: @Html.CascadingDropDownListFor(expression:=Function(x) x.AreaList, triggeredByProperty:=Function(x) x.ProjectTypeID, url:=Url.Action("GetAreaByTType", "Project"), ajaxActionParamName:="TType")

Your test project only shows tests for the CascadingDropDownListFor() overload. I feel like the translation should be simple however I keep getting the error: Overload resolution failed because no accessible....

Thank you

smann004 avatar May 25 '17 03:05 smann004

If you use CascadingDropDownList you need to specify both input name and input id properties:

@Html.CascadingDropDownList(
       inputName := "ProjectTypeID",  //name attribute  of the select element 
       inputId := "ProjectTypeID" //id attribute of the select element
       triggeredByProperty := Function(x) x.SelectedCountry,
       url := Url.Action("GetCities", "Home"),
       ajaxActionParamName := "country",
       optionLabel := "Please select a City", 
       disabledWhenParentNotSelected := true,
       htmlAttributes := New With { .class= "form-control" })

alexanderar avatar May 25 '17 15:05 alexanderar

Thank you but I am still getting an error. I believe that the problem has to do with the triggeredByProperty parameter. The function signature specifies string however I would guess that your code is actually needing the model property. Here is the error. Overload resolution failed because no accessible 'CascadingDropDownList' can be called with these arguments: Extension method 'Public Function CascadingDropDownList(inputName As String, inputId As String, triggeredByProperty As String, url As String, ajaxActionParamName As String, [optionLabel As String = ""], [disabledWhenParentNotSelected As Boolean = False], [htmlAttributes As System.Web.Routing.RouteValueDictionary = Nothing]) As System.Web.Mvc.MvcHtmlString' defined in 'Mvc.CascadeDropDown.DropDownListExtensions': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type

smann004 avatar May 25 '17 17:05 smann004

OK, It looks like there is a problem with override resolving. I will try to solve this. In the meantime you can use this override:

@Html.CascadingDropDownList(
       inputName := "AreaList", 
       inputId := "AreaList" 
       triggeredByProperty := "ProjectTypeID", //specify an id of the parent DD
       url := Url.Action("GetAreaByTType", "Project"),,
       ajaxActionParamName := "TType",
       optionLabel := "Please select ...", 
       disabledWhenParentNotSelected := true,
       htmlAttributes := New With { .class= "form-control" })

alexanderar avatar May 25 '17 18:05 alexanderar

If "ProjectTypeID" is not a property of Model (src), then will result in System.NullReferenceException in

        private static string GetPropStringValue(object src, string propName)
        {
            string stringVal = null;
            if (src != null)
            {
                //this line will throw exception
                ***var propVal = src.GetType().GetProperty(propName).GetValue(src, null);***
                stringVal = propVal != null ? propVal.ToString() : null;
            }
            return stringVal;
        }

It's better to check whether the property is existing or add a try/catch block to set stringVal = null.

Li-Yanzhi avatar May 18 '19 07:05 Li-Yanzhi