我正在研究一个报告时间报告的项目。现在我正在研究一个可以报告假期的功能。从迄今为止。但是当你只需点击发送按钮而不必在我的两个字段中输入任何内容时,它将因空的异常而崩溃。asp.net c#如何处理null?
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(DateTime fromDate, DateTime toDate, string comment)
{
using (IDatabaseLayer db = new DatabaseLayer())
{
if (fromDate != null || toDate != null)
{
db.InsertVacation(Constants.CurrentUser(User.Identity.Name), fromDate, toDate, comment);
ViewData.Model = db.GetUserVacations(Constants.CurrentUser(User.Identity.Name));
}
}
SendVacationMail(fromDate, toDate, comment);
ViewData["posted"] = true;
return View();
}
调试它时,它不会碰到这个代码块,除非我的字段中有值。
@codroipo但如果我的值为null,它甚至不会击中该块。 –
使用'&&''opereator而不是'||' – Sybren
@Sybren不管我使用什么操作符,如果我的字段为空,并且我按下提交,它不会碰到这个块 –