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

[Solved] how to display text and bind to primary key in MVC 3 comboboxfor

2 Answers 165 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
ronald
Top achievements
Rank 1
ronald asked on 06 Apr 2012, 02:35 AM

In MVC3 app, how to display the text (say product name) but at the same time bind the value to its primary key (say product id). here is my html combo box

   

@(Html.Telerik().ComboBoxFor(model =>
model.ProductId)<BR>           
.AutoFill(true)<BR>           
.HighlightFirstMatch(true)<BR>           
.DataBinding(binding => binding.Ajax().Select("GetProductsyKeyword",
"Product")))<BR>

 

and the method GetProductsyKeyword returns:

<P></P>
<P>    return new JsonResult { Data = new SelectList(Products,
"Id", "Description") };<BR></P>

 

The above code is ok for a newly created product but not when load and display the data from database as the combo box has no idea how to display the text. please help!

2 Answers, 1 is accepted

Sort by
0
Zinoviy
Top achievements
Rank 1
answered on 20 Apr 2012, 09:07 AM
I have the same problem. I solved it by adding one element in a list of items.

Html.Telerik().ComboBoxFor(m => m.Filter.ResponsibleId)
     .Items(i => i.Add().Value(Model.Filter.ResponsibleId.ToString()).Text(Model.Filter.ResponsibleName))
     .DataBinding(binding => binding.Ajax().Select("Users", "Default"))
     .Render();

But honestly, I think it is a incorrect.
0
Georgi Krustev
Telerik team
answered on 20 Apr 2012, 09:55 AM
Hello Zinoviy,

 
We decided to not get into account input text when first bind the combobox if it uses Ajax binding. I other words you can define the combobox like this:

Html.Telerik().ComboBoxFor(m => m.Filter.ResponsibleId)
     .InputHtmlAttributes(new { value = Model.Filter.ResponsibleName })
     .DataBinding(binding => binding.Ajax().Select("Users", "Default"))
     .Render();
When end user clicks the combobox, it will show all items without filtering them, which is the expected behavior. You can try the SP of the Telerik Extensions for ASP.NET MVC released on 04/19/2012 (2012.1.419).

Regards,
Georgi Krustev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
ComboBox
Asked by
ronald
Top achievements
Rank 1
Answers by
Zinoviy
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or