如何将默认值设置为DateTime参数 [英] How to set a default value to a DateTime parameter
本文介绍了如何将默认值设置为DateTime参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的MVC应用程序中,我想将默认值设置为 DateTime
参数。
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();
}
推荐答案
您不能设置 null
到 DateTime
,但您可以使用 Nullable DateTime
参数:
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();
}
这篇关于如何将默认值设置为DateTime参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文