[RESOLVED]Using viewbags How to set the condition for two drops….after selecting minsalary then maxsalary should start the same range from what i selected the minlakhs dropdown

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….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>

 

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……

These are the viewbags am getting from the controller

 public void GetAnnualsalarylakhs()
        {
            List<string> Annualsalarylakhs = new List<string>();
            Annualsalarylakhs.Add("<0.5");
            Annualsalarylakhs.Add("1");
            Annualsalarylakhs.Add("2");
            Annualsalarylakhs.Add("3");
            Annualsalarylakhs.Add("4");
            Annualsalarylakhs.Add("5");
            Annualsalarylakhs.Add("6");
            Annualsalarylakhs.Add("7");
            Annualsalarylakhs.Add("8");
            Annualsalarylakhs.Add("9");
            Annualsalarylakhs.Add("10");
            Annualsalarylakhs.Add("11");
            Annualsalarylakhs.Add("12");
            Annualsalarylakhs.Add("13");
            Annualsalarylakhs.Add("14");
            Annualsalarylakhs.Add("15");
            Annualsalarylakhs.Add("16");
            Annualsalarylakhs.Add("17");
            Annualsalarylakhs.Add("18");
            Annualsalarylakhs.Add("19");
            Annualsalarylakhs.Add("20");
            Annualsalarylakhs.Add("21");
            Annualsalarylakhs.Add("22");
            Annualsalarylakhs.Add("23");
            Annualsalarylakhs.Add("24");
            Annualsalarylakhs.Add("25");
            Annualsalarylakhs.Add("26");
            Annualsalarylakhs.Add("27");
            Annualsalarylakhs.Add("28");
            Annualsalarylakhs.Add("29");
            Annualsalarylakhs.Add("30");
            Annualsalarylakhs.Add("31");
            Annualsalarylakhs.Add("32");
            Annualsalarylakhs.Add("33");
            Annualsalarylakhs.Add("34");
            Annualsalarylakhs.Add("35");
            Annualsalarylakhs.Add("36");
            Annualsalarylakhs.Add("37");
            Annualsalarylakhs.Add("38");
            Annualsalarylakhs.Add("39");
            Annualsalarylakhs.Add("40");
            Annualsalarylakhs.Add("41");
            Annualsalarylakhs.Add("42");
            Annualsalarylakhs.Add("43");
            Annualsalarylakhs.Add("44");
            Annualsalarylakhs.Add("45");
            Annualsalarylakhs.Add("46");
            Annualsalarylakhs.Add("47");
            Annualsalarylakhs.Add("48");
            Annualsalarylakhs.Add("49");
            Annualsalarylakhs.Add("50");
            ViewBag.Annualsalarylakhs1 = Annualsalarylakhs;
        }

        public void GetAnnualSalaryThousands()
        {
            List<string> AnnualSalaryThousands = new List<string>();
            AnnualSalaryThousands.Add("10000");
            AnnualSalaryThousands.Add("20000");
            AnnualSalaryThousands.Add("30000");
            AnnualSalaryThousands.Add("40000");
            AnnualSalaryThousands.Add("50000");
            AnnualSalaryThousands.Add("60000");
            AnnualSalaryThousands.Add("70000");
            AnnualSalaryThousands.Add("80000");
            AnnualSalaryThousands.Add("90000");
            ViewBag.AnnualSalaryThousands1 = AnnualSalaryThousands;
        }

Please help me anyone……..

Thanks in advance………..

Hi Madhavaram,

For this requirement, it is similar to the cascading DropDown list. Please refer to this link below to achieve that:

# Cascading Dropdown List With MVC, LINQ to SQL and AJAX

http://www.codeproject.com/Articles/730953/Cascading-Dropdown-List-With-MVC-LINQ-to-SQL-and-A

Best Regards

Starain

Am not able to solve it using this article……..can u solve it by using this

these are my two methods that am using in the controller  and binding to return to the view…….

 public void GetAnnualsalarylakhs()
        {
            List<string> Annualsalarylakhs = new List<string>();
            Annualsalarylakhs.Add("<0.5");
            Annualsalarylakhs.Add("1");
            Annualsalarylakhs.Add("2");
            Annualsalarylakhs.Add("3");
            Annualsalarylakhs.Add("4");
            Annualsalarylakhs.Add("5");
            Annualsalarylakhs.Add("6");
            Annualsalarylakhs.Add("7");
            Annualsalarylakhs.Add("8");
            Annualsalarylakhs.Add("9");
            Annualsalarylakhs.Add("10");
            Annualsalarylakhs.Add("11");
            Annualsalarylakhs.Add("12");
            Annualsalarylakhs.Add("13");
            Annualsalarylakhs.Add("14");
            Annualsalarylakhs.Add("15");
            Annualsalarylakhs.Add("16");
            Annualsalarylakhs.Add("17");
            Annualsalarylakhs.Add("18");
            Annualsalarylakhs.Add("19");
            Annualsalarylakhs.Add("20");
            Annualsalarylakhs.Add("21");
            Annualsalarylakhs.Add("22");
            Annualsalarylakhs.Add("23");
            Annualsalarylakhs.Add("24");
            Annualsalarylakhs.Add("25");
            Annualsalarylakhs.Add("26");
            Annualsalarylakhs.Add("27");
            Annualsalarylakhs.Add("28");
            Annualsalarylakhs.Add("29");
            Annualsalarylakhs.Add("30");
            Annualsalarylakhs.Add("31");
            Annualsalarylakhs.Add("32");
            Annualsalarylakhs.Add("33");
            Annualsalarylakhs.Add("34");
            Annualsalarylakhs.Add("35");
            Annualsalarylakhs.Add("36");
            Annualsalarylakhs.Add("37");
            Annualsalarylakhs.Add("38");
            Annualsalarylakhs.Add("39");
            Annualsalarylakhs.Add("40");
            Annualsalarylakhs.Add("41");
            Annualsalarylakhs.Add("42");
            Annualsalarylakhs.Add("43");
            Annualsalarylakhs.Add("44");
            Annualsalarylakhs.Add("45");
            Annualsalarylakhs.Add("46");
            Annualsalarylakhs.Add("47");
            Annualsalarylakhs.Add("48");
            Annualsalarylakhs.Add("49");
            Annualsalarylakhs.Add("50");
            ViewBag.Annualsalarylakhs1 = Annualsalarylakhs;
        }

        public void GetAnnualSalaryThousands()
        {
            List<string> AnnualSalaryThousands = new List<string>();
            AnnualSalaryThousands.Add("10000");
            AnnualSalaryThousands.Add("20000");
            AnnualSalaryThousands.Add("30000");
            AnnualSalaryThousands.Add("40000");
            AnnualSalaryThousands.Add("50000");
            AnnualSalaryThousands.Add("60000");
            AnnualSalaryThousands.Add("70000");
            AnnualSalaryThousands.Add("80000");
            AnnualSalaryThousands.Add("90000");
            ViewBag.AnnualSalaryThousands1 = AnnualSalaryThousands;
        }

 [HttpGet]
        public ActionResult CompanyVacancy()
        {

           
            GetAnnualsalarylakhs();
            GetAnnualSalaryThousands();
            GetState();
            return View();
        }

this is the view 

<dl>
<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>

how to check  the condition based on minimum salary dropdown how to get the max salary dropdown values………

please help me how to get the condition………..

thanks in advance 

Hi Madhavaram,

Please modify your code like this:

 public List<string> GetAnnualsalarylakhs(string min)
        {
           List<string> Annualsalarylakhs = new List<string>();
           int minValue=1;
           if(min==null)
            {
                Annualsalarylakhs.Add("<0.5");
            }
            else
            {
               minValue=int.Parse(min);
               for(int i=minValue+1;i<50;i++)
                  {
                     Annualsalarylakhs.Add(i.ToString());
                  }

            }            
            return Annualsalarylakhs;
        }
 public List<string> GetAnnualSalaryThousands(int min=9999)
        {
            List<string> AnnualSalaryThousands = new List<string>();
            for(int i=min+1;i<90000;i+=10000)
            {
                 AnnualSalaryThousands.Add(i.ToString());
            }
            AnnualSalaryThousands.Add("10000");
            AnnualSalaryThousands.Add("20000");
            AnnualSalaryThousands.Add("30000");
            AnnualSalaryThousands.Add("40000");
            AnnualSalaryThousands.Add("50000");
            AnnualSalaryThousands.Add("60000");
            AnnualSalaryThousands.Add("70000");
            AnnualSalaryThousands.Add("80000");
            AnnualSalaryThousands.Add("90000");
            return AnnualSalaryThousands;
        }

[HttpGet]
        public ActionResult CompanyVacancy()
        {
   
           ViewBag.Annualsalarylakhs1= GetAnnualsalarylakhs();
           ViewBag.AnnualSalaryThousands1 = GetAnnualSalaryThousands();
            GetState();
            return View();
        }
$(function () {
        $("#txtMinimumLakhs").change(function () {
                 var selectedItem = $(this).val();
               var ddlMax = $("#txtMaximumLakhs");
               $.ajax({
                 cache: false,
                 type: "GET",
                 url: "@(Url.Action("GetAnnualsalarylakhs"))",
                    data: { "min": selectedItem },
                    success: function (data) {                       
                        $.each(data, function (id, option) {
                            ddlMax .append($('<option></option>').val(data).html(data));
                        });  
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to retrieve states.'); 
                    }
                });
            });
        });
     </script> 

Note: This is just a simple and part code, you need modify it.

Best Regards

Starain

Try  this

    $(document).ready(function () {
        $("#AnnualSalary_MinimumLakhs").change(function () {
            var min = parseInt($(this).val());
            $("#AnnualSalary_MaximumLakhs option").each(function () {               
                $(this).remove();
            });
            $("#DaysAnnualSalary_MinimumLakhs option").each(function () {
                if (min <= parseInt($(this).val()))
                    $("#AnnualSalary_MaximumLakhs").append($(this).clone());
            });
        });
    });

Leave a Reply