I am looking to force a submit once a user selects a value for DropDownList as long as it is not ‘Custom’. If it is ‘Custom’ I don’t want the form submitted. Instead I would like to have the startingDate and endingDate to show. However, I don’t want the startingDate or endingDate to show unless Custom was selected. I am thinking this has to be done with jQuery or JavaScript. Can anybody tell me how to achieve this?
Here is the code I have in the controller to pass the starting date, ending date and list for the drop down to the view:
List<SelectListItem> rangeList = new List<SelectListItem>();
rangeList.Add(new SelectListItem { Text = "Today", Value = "Today" });
rangeList.Add(new SelectListItem { Text = "Yesterday", Value = "Yesterday" });
rangeList.Add(new SelectListItem { Text = "Past 7 Days", Value = "Past 7 Days" });
rangeList.Add(new SelectListItem { Text = "Past 30 Days", Value = "Past 30 Days" });
rangeList.Add(new SelectListItem { Text = "Last Month", Value = "Last Month" });
rangeList.Add(new SelectListItem { Text = "Custom", Value = "Custom" });
ViewBag.rangeList = rangeList;
ViewBag.startingDate = startingDate;
ViewBag.endingDate = endingDate;
ViewBag.specifiedRange = specifiedRange;
EDIT
I modified my code to show an attempt at adding the script. Below is the code I have in the view now. It is not doing anything.
<script type="text/javascript">
$("#range").change(function ()
{
if ($(this).val() == "Custom")
{
$("p.down").toggle();
}
else
{
$("form").submit();
}
});
</script>
@using (Html.BeginForm())
{
<p>
@Html.DropDownList("specifiedRange", new SelectList(
ViewBag.rangeList as System.Collections.IEnumerable,
"Text",
"Value",
new { @Id = "range" }))
</p>
<p class = "down">
Starting Date: @(Html.Telerik().DateTimePicker().Name("startingDate"))
Ending Date: @(Html.Telerik().DateTimePicker().Name("endingDate"))
<input type="submit" value="GO" />
</p>
}
Yes it needs to be done with jquery.Something like this:
And class “down” to your
<p>that you want to show.