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

binding to int? not working

6 Answers 416 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
MelF
Top achievements
Rank 1
MelF asked on 26 Sep 2012, 05:50 PM
if I bind to a nullable integer which has an initial value of null, the combobox always returns a null value value to the controller, even if a value has been selected. if I change it to work with a  standard integer, it returns the correct value, but it also sets an initial value in the list which I do not want.

thoughts?

no initial value, but not updating either: public int? QuoteId { get; set; } 
returning values as expected, but populating 0: public int QuoteId { get; set; } 

@(Html.Kendo().ComboBoxFor(m => m.QuoteId)
                  .DataSource(dataSource => dataSource
                                                .Read(read => read.Action("GetQuoteList", "Payment")))
                  .DataTextField("Text")
                  .DataValueField("Value")
                  )

6 Answers, 1 is accepted

Sort by
0
Murat
Top achievements
Rank 1
answered on 29 Oct 2012, 01:20 PM
Even if you change int to nullable int into your demo. GridForeignKey.aspx getting same behaviour.
0
MelF
Top achievements
Rank 1
answered on 19 Nov 2012, 03:59 PM
same difference. Any ideas? this is continuing to be a problem for me.
0
Mind of Neeko
Top achievements
Rank 2
answered on 30 Nov 2012, 10:45 PM
MelF,

We had this same Issue and this was our "Work Around".

I am going to make a few assumptions.

1. You are using a [site].Domain and [site].WebUI project set up in your solution.
2. You are using Concrete/Abstract/Entity in your .Domain project
3. You are using Model/View/Controller in your .WebUI project.
4. You are performing your queries in the Controller against your .Domain repositories.
5. I haven't lost you so far :D.

So what we did was:

1. In the Controller Action to "GET" the data for our Combo Box we added a "null" value
    a. ex. Select CompanyID = a.CompanyID == null ? 0 : a.CompanyID
        i. This basically populates your model with a 0 if the DB result is NULL.

2. In the Page in which the Combo Box lives you will now see a "0" value if the DB value is NULL
    a. Making this more user friendly is another topic.

3. In the Controller Action that handles the "POST" of your form we do the opposite to 1.
    a. ex Insert CompanyID = a.CompanyID == 0 ? null : a.CompanyID


We basically forced the Combo Box to process a "0" record if one does not exist, then on the Insert Portion we remove the "0" and set it back to null.
0
Rock
Top achievements
Rank 1
answered on 07 Jan 2013, 12:59 PM
i have same error.

i using your solution.
but a problem,
When opening a new form , not saving data of combobox!
No principled solution?
0
Josiah
Top achievements
Rank 1
Veteran
answered on 13 Feb 2013, 09:01 PM
unfortunately none of these solutions would work if you have data with an Id = 0.  may be poor data design, but very common. Sending a project directly to telerik.  I will report back with any findings
0
Josiah
Top achievements
Rank 1
Veteran
answered on 15 Feb 2013, 03:23 PM
this issue is easily resolved with 2 modifications:

  • DO NOT name your drop-down
  • add this to your validation

<script>
    $.validator.setDefaults({
        ignore: ""
    });
</script>
Tags
ComboBox
Asked by
MelF
Top achievements
Rank 1
Answers by
Murat
Top achievements
Rank 1
MelF
Top achievements
Rank 1
Mind of Neeko
Top achievements
Rank 2
Rock
Top achievements
Rank 1
Josiah
Top achievements
Rank 1
Veteran
Share this question
or