Hi rani,
Check this example. Now please take its reference and correct your code.
Namespaces
using System.Linq;
using System.Globalization;
using Microsoft.AspNetCore.Mvc.Rendering;
Model
public class MonthDayModel
{
public SelectList Months { get; set; }
public SelectList Days { get; set; }
}
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
MonthDayModel model = new MonthDayModel();
model.Months = new SelectList(PopulateMonths(), "Value", "Text");
return View(model);
}
[HttpPost]
public IActionResult Index(string month)
{
MonthDayModel model = new MonthDayModel();
model.Months = new SelectList(PopulateMonths(), "Value", "Text");
if (month != "Please select")
{
//int monthNumber = DateTime.ParseExact(month, "MMMM", CultureInfo.CurrentCulture).Month;
int monthNumber = DateTimeFormatInfo.CurrentInfo.MonthNames.ToList().IndexOf(month) + 1;
List<SelectListItem> days = new List<SelectListItem>();
for (int i = 1; i <= DateTime.DaysInMonth(DateTime.Now.Year, monthNumber); i++)
{
days.Add(new SelectListItem { Value = i.ToString(), Text = i.ToString() });
}
model.Days = new SelectList(days, "Value", "Text");
}
return View(model);
}
private static List<SelectListItem> PopulateMonths()
{
List<SelectListItem> months = new List<SelectListItem>();
months.Add(new SelectListItem { Value = "1", Text = "January" });
months.Add(new SelectListItem { Value = "2", Text = "February" });
months.Add(new SelectListItem { Value = "3", Text = "March" });
months.Add(new SelectListItem { Value = "4", Text = "April" });
months.Add(new SelectListItem { Value = "5", Text = "May" });
months.Add(new SelectListItem { Value = "6", Text = "June" });
months.Add(new SelectListItem { Value = "7", Text = "July" });
months.Add(new SelectListItem { Value = "8", Text = "August" });
months.Add(new SelectListItem { Value = "9", Text = "September" });
months.Add(new SelectListItem { Value = "10", Text = "October" });
months.Add(new SelectListItem { Value = "11", Text = "November" });
months.Add(new SelectListItem { Value = "12", Text = "December" });
return months;
}
}
View
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model Bind_DropDownList_Core.Model.MonthDayModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form asp-action="Index" asp-controller="Home" method="post">
@if (Model.Months != null)
{
<span>Month: </span>
<select id="ddlMonths" asp-items="Model.Months">
<option value="0">Please select</option>
</select>
}
<input type="hidden" name="month" />
<input type="submit" value="Submit" />
@if (Model.Days != null)
{
<br />
<span>Day: </span>
<select id="ddlDays" asp-items="Model.Days">
<option value="0">Please select</option>
</select>
}
</form>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("body").on("change", "#ddlMonths", function () {
$("input[name=month]").val($(this).find('option:selected').text());
});
</script>
</body>
</html>
Screenshot