alexanderar / Mvc.CascadeDropDown

MIT License
26 stars 12 forks source link

Is it easily customisable for date picking? :) #10

Closed JeanCollas closed 8 years ago

alexanderar commented 8 years ago

You can use it to select any type of data that could be serialized to string. I don't know if it is a right choice for date picking because there are plenty of really great datepickers that were designed exactry for that purpose. Anyway, all you need to do is to provide a valid collection of SelectListItem objects:

For example you have a dropdown that you use to select month and you want dynamically populate days dropdown:

public ActionResult GetDatesForMonth(int month)
{
     // validate that month is valid, if it is:
     var date = new DateTime(2016,month,1);
     var endDate = new DateTime(2016, month%13, 1);
     var model = new List<SelectListItem>();
    do
     {
              model.Add(new SelectListItem{Text = date.Day.ToString(), Value = date.Day.ToString() });
              date.AddDays(1);
     }
     while( date < endDate )
     return new JsonResult(model, JsonRequestBehavior.AllowGet);
}

In the view:

@Html.DropDownListFor(m=>m.Month, Model.Months,
"Please select a Month", new {@class="form-control"})

@Html.CascadingDropDownListFor( 
      expression: m => m.Day, 
      triggeredByProperty: m => m.Month,  //Parent property that trigers dropdown data loading
      url: Url.Action("GetDatesForMonth", "Home"),  //Url of action that returns dropdown data
      actionParam: "month",   //Parameter name for the selected parent value that url action receives
      optionLabel: "Please select a Date", // Option label
      disabledWhenParrentNotSelected: true, //If true, disables dropdown until parrent dropdown selected
      htmlAttributes: new { @class = "form-control" })