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

DropDownListFor Events in Razor Pages

5 Answers 2204 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
AJ
Top achievements
Rank 1
AJ asked on 25 Jul 2018, 04:01 PM

I'm trying to add Change Event for DropDownListFor in Razor Pages but facing an issue - "Cannot use lambda expression..."

@(Html.Kendo().DropDownListFor(model => model.CaseSourceCode)

                         .BindTo(ViewBag.LookupSourceTypes)

                         .Events(e => e.Change("OnChange")

)

 

If I don't add the Events line the DropDownListFor works just fine.

 

5 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 30 Jul 2018, 10:39 AM
Hello Raju,

The demonstrated usage seems correct, besides the fact that one parenthesis is missing from the implementation:

@(Html.Kendo().DropDownListFor(model => model.Customer)
                             .BindTo(ViewBag.LookupSourceTypes)
                             .Events(e => e.Change("OnChange"))
    )

Could you make sure that it's present at your end and let us know if the issue still persists?


Regards,
Nencho
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
AJ
Top achievements
Rank 1
answered on 30 Jul 2018, 02:38 PM

Thank you Nencho for your response. I have the parenthesis correct on my development machine. I cannot copy paste the code from there since that machine has no internet access.

So for the following code -

@(Html.Kendo().DropDownListFor(model => model.CaseSourceCode)
                         .BindTo(ViewBag.LookupSourceTypes)
                         .Events(e => e.Change("OnChange"))
)

the error is "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

If I change the code to -

@(Html.Kendo().DropDownListFor(model => model.CaseSourceCode)

                          .Events(e => e.Change("OnChange"))

                          .BindTo(ViewBag.LookupSourceTypes)                       
)

The error goes away however on the UI the dropdown list appears as a text box with a value 0 and in the console I see the error -

"Uncaught ReferenceError: OnChange is not defined"

0
AJ
Top achievements
Rank 1
answered on 30 Jul 2018, 02:42 PM

Forgot to mention that I have the script for OnChange

<script type="text/javascript">

function OnChange(e){

....

}

</script>

0
Nencho
Telerik team
answered on 02 Aug 2018, 10:44 AM
Hello Raju,

Thank you for the additionally provided information that made the problem clear.

The reason for the expectation is that the ViewBag variable is not cast to any type, hence the widget is not aware of the type. In order to overcome the issue, you can cast the type to IEnumerable:

@(Html.Kendo().DropDownListFor(model => model.CaseSourceCode)
                                 .BindTo((System.Collections.IEnumerable)ViewBag.LookupSourceTypes)
                                 .Events(e => e.Change("OnChange"))
)


This topic is also discussed in the following thread:

https://www.telerik.com/forums/binding-to-viewbag-in-razor-pages

Hope this would help.

Regards,
Nencho
Progress Telerik
Get quickly and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
1
AJ
Top achievements
Rank 1
answered on 02 Aug 2018, 01:46 PM

Appreciate your help. However I'm still getting the "Uncaught ReferenceError: OnChange is not defined" error after I tried your solution. As per Telerik's support I moved my script block to the top of the page and voila it started working.

Thanks again to you and your team for helping me resolve the issue.

So the complete solution is -

<script type="text/javascript">
function OnChange(e){
....
}
</script>

<form>

  @(Html.Kendo().DropDownListFor(model => model.CaseSourceCode)
                          .Events(e => e.Change("OnChange"))
                          .BindTo(ViewBag.LookupSourceTypes)                       
)

</form>

 

Tags
DropDownList
Asked by
AJ
Top achievements
Rank 1
Answers by
Nencho
Telerik team
AJ
Top achievements
Rank 1
Share this question
or