[RESOLVED]Set the condition for two Dropdownlists using viewbag

Hi am new to mvc………..am using two dropdowns ( one is for minimum salary and the other is max salary) getting data from the controller using viewbag…….but i need to set the condition min salary should be less than max salary  for two drop downs………..can
anyone help me to solve it……..

THIS IS MY VIEW

 <dt>Annual salary</dt>
                                                    <dd> <%= Html.DropDownListFor(model => model.AnnualSalary.MinimumLakhs, new SelectList(ViewBag.Annualsalarylakhs1),"Lakhs",new {style="display:inline; width:90px;",@id="txtMinimumLakhs",@class="mid-size mr10",@placeholder="Laks"})%>
                                                        <%= Html.DropDownListFor(model => model.AnnualSalary.MinimumThousands, new SelectList(ViewBag.AnnualSalaryThousands1),"Thousands",new {style="width:90px;",@id="txtMinimumThousands",@class="mid-size",@placeholder="Thousands"})%></dd>
                                                    <dt>&nbsp;</dt>
                                                    <dt>To</dt>
                                                    <dt>&nbsp;</dt>
                                                    <dd><%= Html.DropDownListFor(model => model.AnnualSalary.MaximumLakhs, new SelectList(ViewBag.Annualsalarylakhs1),"Lakhs",new {style="display:inline; width:90px;",@id="txtMaximumLakhs",@placeholder="Laks",@class="mid-size mr10"})%>
                                                        <%= Html.DropDownListFor(model => model.AnnualSalary.MaximumThousands, new SelectList(ViewBag.AnnualSalaryThousands1),"Thousands",new {style="display:inline; width:90px;",@id="txtMaximumThousands",@placeholder="Thousands",@class="mid-size"})%></dd>
                                                    </dl>

Thanks in advance………

I have 3 suggestions that you could try

1) on-select-change, check if the minimum value is less than the maximum value.  If false, disable the Submit button to prevent posting and display alert to correct the values.

2) on-form-submit, check if the minimum value is less than the maximum value.  If false, prevent submission and display alert to correct values.

3) Check values at the  controller level, and if false, return a ModelStateError message back to view (I would not let your post get to controller though)

I would personally use #2 as it only requires checking at submission time, and doesn’t send any data to the controller if your validation fails

$("form").submit(function( event ) {
  if ($("#MinimumLakhs").val() > $("#MaximumLakhs").val()) {
alert("Minimum Lakhs must be less than maximum Lakhs");
event.preventDefault();
}

// do the same for "Thousands" if necessary });

When you submit your form, this event handler gets triggered.  It will then check your minimum Lakh value against the maximum value.  If it is false (greater than the maximum), it will alert the user to correct it.  The "preventDefault" method cancels the
submit event, preventing the form from being passed to controller.

Thank  u JohnLocke

 

Thank u johnLocke but i need the condition as 

When i select the a value in the dropdown  minlakhs then the values in the  maxlakhs drop down should start the range from what i selected the minlakhs dropdown …….

For example when i select minlakhs dropdown value as 4lakhs the values in the max lakhs drop down should start the range from 4lakhs……

How can i do this using viewbag……..

Please help me anyone……..

Thanks in advance………..

Leave a Reply