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

Bind DropDropDownListFor in ListView Template Asp.net Core

6 Answers 384 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 29 Jan 2020, 05:10 PM
I have a list view template that contains a Kendo DropDownListFor.  I can't figure out how to bind to a property of my model. The code that I tried is below  I commented out the bindto.  My model is ContactViewModel with a property of a List of ContactAddress with properties of FullStreetAddress and ContactAddressId.   I am not sure how to set this up.  Any help would be appreciated.
<script type="text/x-kendo-template" id="contacttemplate">
        <div>
            @(Html.Kendo().DropDownListFor(m=>m.SelectedContactAddressId)
   .Name("addressDropDown")
   .DataTextField("FullStreetAddress")
   .DataValueField("ContactAddressId")
    //.BindTo(#:ContactAddresses#) // Normally non template .BindTo(Model.ContactAddresses) This is a error also
    .ToClientTemplate()
     )

6 Answers, 1 is accepted

Sort by
0
Carlos
Top achievements
Rank 1
answered on 29 Jan 2020, 05:46 PM
I messed up the question should be Concerning a DropDownList
@(Html.Kendo().DropDownList()
.Name("addressDropDown")
.DataTextField("FullStreetAddress")
.DataValueField("ContactAddressId")
//.BindTo("#:ContactAddresses#")
.ToClientTemplate()
)
0
Ivan Danchev
Telerik team
answered on 03 Feb 2020, 04:25 PM

Hello Carlos,

If the model is declared like this:

@model MyApp.Models.ContactViewModel

You will have access to the ContentAddresses field: 

.BindTo(Model.ContactAddresses)

However, if you are using an IEnumerable of ContactViewModel:

@model IEnumerable<MyApp.Models.ContactViewModel>

you cannot use this approach since the model will be a collection of ContactViewModel.

Evaluating the ContactAddresses field in the template ("#:ContactAddresses#") won't work either since when using server binding (BindTo) the data must already be present on the client. BindTo works with the model directly, e.g.: Model.ContactAddresses, or you can pass a server variable, for instance a ViewBag:

.BindTo((System.Collections.IEnumerable)ViewData["addresses"])

Thus, in this scenario consider using remote binding, instead of server binding with the BindTo method.

Regards,
Ivan Danchev
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
0
Carlos
Top achievements
Rank 1
answered on 03 Feb 2020, 04:34 PM
Thank you for your response.  For now I flattened the data giving me unique rows but appear to be duplicated because of different phone numbers.  Once I get the page roughed in, I will circle back and see if I can make this work the listview with dropdowns and another remote call just for the phone numbers.
0
Carlos
Top achievements
Rank 1
answered on 06 Feb 2020, 07:34 PM

I have had some time to research to add remote binding.  My problem is the Id/Name is not unique and I can not pass a parameter.  The following code creates an invalid template:

                               @(Html.Kendo().DropDownList()
                                        .Name("homePhoneDropdown_#:ContactId#")
                                        .DataTextField("PhoneNumber")
                                        .DataValueField("ContactPhoneId")
                                        .DataSource(source =>source
                                            .Ajax()
                                            .Read(read => read.Action("ReadPhone", "Summary", new { Area = "Summary", id = #:ContactId# }))
                                        )
                                        .HtmlAttributes(new { style = "width: 100%;", @class="dropdown" })
                                    .ToClientTemplate()
                                    )

I need to pass the current ContactId to get the correct phones numbers from the service.  I have tried a literal 7522, #:ContactId#, #=ContactId# with and without quotes.  I am not sure were the error is in the ID or the read.  Any help would be appreciated.

0
Carlos
Top achievements
Rank 1
answered on 06 Feb 2020, 08:24 PM
My mistake the id is #=.  Not sure about using the #=ContactId# for the parameter.  Using the literal everything works.  Just need to pass the parameter value of ContactId.  At least I am making progress.
0
Carlos
Top achievements
Rank 1
answered on 06 Feb 2020, 08:32 PM
I solved the problem I changed the parameter from long to string and changed the code to new { Area = "Summary", id = "#=ClientId#"}.  In the controller parse the string to a long and pass that to my ling where clause.  All is good.  I probably should check for null.  Thanks for the assist earlier.
Tags
ListView
Asked by
Carlos
Top achievements
Rank 1
Answers by
Carlos
Top achievements
Rank 1
Ivan Danchev
Telerik team
Share this question
or