我想知道是否有人可以在这里帮助我。我想给出这样的日期格式:"MM/dd/yyyy"
我知道查询字符串中的所有格式都应为int或string。这是我的url.action:
@Html.PagedListPager(Model, page => Url.Action("Index", new
{
page,
creationDate = ViewBag.Date.ToString(),
sortOrder = ViewBag.CurrentSort,
AccountIdFilter = ViewBag.AccountIdFilter
}))
问题出在creationDate = ViewBag.Date.ToString()
上。这将转换为字符串,但不是MM/dd/yyyy
格式。这是我的控制器:
public ActionResult Index(string accountIdFilter,DateTime? creationDate, int page=1, Sort sortOrder =Sort.Descending)
{
int take = 2;
DateTime date;
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = sortOrder == Sort.Descending ? Sort.Assending : Sort.Descending;
ViewBag.AccountIdFilter = accountIdFilter;
if (!creationDate.HasValue)
{
date = DateTime.ParseExact("2020-05-08", "yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture);
}
else
{
date = DateTime.ParseExact(creationDate.ToString(), "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
}
ViewBag.Date = date;
BankDepositHistoryListDTO data;
try
{
data = BankDepositHistoryServices.GetAllByPagination(sortOrder, accountIdFilter, take, creationDate, page);
}
catch (Exception ex)
{
logger.Error(ex.ToString());
data = BankDepositHistoryServices.GetAllByPagination(sortOrder, accountIdFilter, take, creationDate, 1);
page = 1;
}
var result = data.BankDepositHistoriyItems;
int count = data.Count;
var resultPagedList = new StaticPagedList<BankDepositHistoryDTO>(result, page, take, count);
return View(resultPagedList);
}
问题在这里:
else
{
date = DateTime.ParseExact(creationDate.ToString(), "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
}
那一行我遇到了这个异常:String was not recognized as a valid DateTime.
creationDate.ToString()
的输出为"5/8/2020 12:00:00 AM"
。
所以我该怎么做?
非常感谢