简体   繁体   中英

How to set a default value to a DateTime parameter

In my MVC application I want to set default values to the DateTime parameter.

[HttpPost]
public ActionResult BudgetVSActualTabular(DateTime startDate)
{
    var Odata = _db.sp_BudgetedVsActualTabular(startDate).ToList();    
    string[] monthName = new string[12];    
    for (int i = 0; i < 12;i++ )
    {
        DateTime date = startDate;
        date = date.AddMonths(i);
        monthName[i] = date.ToString("MMMM") + " " + date.Year.ToString();     
    }

    ViewBag.startDate = new SelectList(_db.DaymonFinancialYears, "startDate", "DateRange");    
    var MonthName = monthName.ToList();
    ViewBag.Bdata = Odata;
    ViewBag.Cdata = MonthName;
    return View();
}

You cannot set a null to a DateTime but you can use a Nullable DateTime parameter instead:

[HttpPost]
public ActionResult BudgetVSActualTabular(DateTime? startDate = null )
{ 
    if (startDate == null)
    {
        startDate  = new DateTime(2016, 06, 01);
    }
    //You should pass startDate.Value
    var Odata = _db.sp_BudgetedVsActualTabular(startDate.Value).ToList();

}

You can use the default keyword with this syntax

public ActionResult BudgetVSActualTabular(DateTime startDate = default(DateTime))

This will make possible to call the method without passing any parameter and inside your method the startDate variable will be equal to DateTime.MinValue

If you need the default to be a specific date instead of DateTime.MinValue you could write a simple test

public ActionResult BudgetVSActualTabular(DateTime startDate = default(DateTime))
{
    if(startDate == DateTime.MinValue)
        startDate = new DateTime(2014,6,1);

    // After the check for a missing parameter pass the startDate as before
    var Odata = _db.sp_BudgetedVsActualTabular(startDate).ToList();
    .....
}

Named and optional (default) parameters are available starting from C# 4.0. in case you're using an older version, you may overload your method like:

 public ActionResult BudgetVSActualTabular()
 {
      return BudgetVSActualTabular(new DateTime(2014,6,1));
 }

I would suggest setting the DateTime to be nullable public ActionResult BudgetVSActualTabular(DateTime? startDate) .

Inside your controller you can use DateTime.HasValue to set a default if the DateTime is null.

var nonNullableDate = startDate.HasValue ? startDate.Value : new DateTime();

[HttpPost]
public ActionResult BudgetVSActualTabular(DateTime? startDate)
{
    var nonNullableDate = startDate.HasValue ? startDate.Value : new DateTime();
    var Odata = _db.sp_BudgetedVsActualTabular(nonNullableDate).ToList();    
    string[] monthName = new string[12];    
    for (int i = 0; i < 12;i++ )
    {
        DateTime date = nonNullableDate;
        date = date.AddMonths(i);
        monthName[i] = date.ToString("MMMM") + " " + date.Year.ToString();     
    }

    ViewBag.startDate = new SelectList(_db.DaymonFinancialYears, "startDate", "DateRange");    
    var MonthName = monthName.ToList();
    ViewBag.Bdata = Odata;
    ViewBag.Cdata = MonthName;
    return View();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM