Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  59] [ 5]  / answers: 1 / hits: 21689  / 12 Years ago, mon, january 14, 2013, 12:00:00

I have a SelectList representing a delivery type for an order.



The delivery type reference data has the usual code/description, but also an additional boolean property which indicates if further information needs to be entered for the type selected.



So for Emergency deliveries additional data is required. The additional data entry fields would be set visible if Emergency was selected, otherwise hidden



My ViewModel contains <List>ReferenceDeliveryTypes which contains the 3 properties.
I have created a SelectListItems from the ViewModel data



@Html.DropDownListFor(model => model.DeliveryTypeCode, 
new SelectList(Model.ReferenceDeliveryTypes as System.Collections.IEnumerable,
DeliveryTypeCode, DeliveryTypeDescription), new { id = ddlDeliveryType })


How can I call a jQuery function on change of the delivery type, pass the selected code and check the Model.ReferenceDeliveryTypes for that code to see if the additional data property is true/false to show/hide the additional fields div?



I have managed to get the jQuery function called to pass the value.



$(function () {

$('#ddlDeliveryType').change(function () {
var value = $(this).val();
alert(value);
});


});


More From » jquery

 Answers
3

I don't know of any way you can do this using a select list but I suggest the following options:




  • Simple but a hack - add a string to the end of DeliveryTypeDescription, for example (emergency delivery) and check for that in your change function

  • Another hack - multiply DeliveryTypeCode by 10 and add 1 on if it's an emergency delivery (and then use mod 10 in your change function)

  • Use an Ajax lookup function

  • Load a JavaScript lookup table with the codes which require an emergency delivery

  • Use a hidden field in your form which contains a string list of the emergency codes with a suitable separator



Good luck



UPDATE
For the hidden field option if you use something like 123|456|789| and then use indexOf having appended a | to the selected ID.


[#80893] Saturday, January 12, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bretd

Total Points: 6
Total Questions: 100
Total Answers: 97

Location: England
Member since Sun, May 21, 2023
1 Year ago
;