【发布时间】:2014-02-26 19:34:40
【问题描述】:
我开发了文本框来添加日期范围以选择该日期范围内的特定记录。 我已经为此编写了 IF ELSE,如果两个文本框都为空,则显示错误消息,但问题是当我第一次调用页面时,它会显示相同的错误。我知道为什么,因为在第一次尝试文本框是空的,但如何控制呢?
public ActionResult ShowMyAtdByDate(String DateFrom, String DateTo)
{
int empId = 0;
int.TryParse((string)Session["Employee"], out empId); // parse variable to int and saves the result in empId
// IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date(DateFrom,DateTo,empId).ToList();
if (DateFrom != "" && DateTo == "" && empId > 0)
{
IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date(DateFrom,null, empId).ToList();
ViewBag.Dates = "Records for" + " " + DateFrom;
return View(MyAtdRecord);
}
else if (DateFrom == "" && DateTo != "" && empId > 0)
{
IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date( null, DateTo, empId).ToList();
ViewBag.Dates = "Records for" + " " + DateTo;
return View(MyAtdRecord);
}
else if (DateFrom != "" && DateTo != "" && empId > 0)
{
IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date(DateFrom, DateTo, empId).ToList();
ViewBag.Dates = "Records from" + " " + DateFrom + " " + "to" + " " + DateTo;
return View(MyAtdRecord);
}
else if (DateFrom == "" && DateTo == "" && empId > 0)
{
IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date(null, null, empId).ToList();
ViewBag.Dates = "No dates selection";
return View(MyAtdRecord);
}
else if(empId <=0 )
{
return RedirectToAction("IsAuth_Page","Home");
}
return View();
}
查看:
@{
var grid = new WebGrid(ViewData.Model, rowsPerPage: 25);
}
@if (Model.Count > 0)
{
<div id="AllMyAtd_ByDate">
@grid.GetHtml(columns: grid.Columns(
grid.Column("EmplID", "Employee ID"),
grid.Column("EmplName", "Employee Name"),
grid.Column("ShiftID", "Shift ID"),
grid.Column("DateVisited", "Date of Visit"),
grid.Column("InTime", "In Time"),
grid.Column("TimeOut", "Time Out"),
grid.Column("OverTime", "Over Time"),
grid.Column("TotalWorkingTime", "Total Working Time")
))
</div>
}
else
{
<h4 class="error">Sorry Record Doesn't Exist for selected date(s)</h4>
}
当我第一次浏览到这个页面时,它会出现,实际上只有当我将两个文本框都留空时才会出现。
【问题讨论】:
标签: c# asp.net asp.net-mvc-3 c#-4.0 if-statement