我已经开发了文本框以添加日期范围,以在该日期范围内选择特定记录。如果两个文本框都是空的,我已经写了如果其他内容,则显示错误消息,但是问题是当我首次拨打电话页面时,它显示相同的错误。我知道为什么,因为在第一次尝试中,文本框是空的,但是如何控制它?
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>
}
当我第一次浏览此页面时,这实际上只有在我将两个文本框留为空时才出现。
用户第一次进入此页面时,它将是一个重新要求。您可以从get方法中删除此逻辑(因为它是第一次时不需要执行)并将其放入后方法中,因为这是当时将在用户已提交表格。
[HttpGet]
public ActionResult ShowMyAtdByDate()
{
int empId = 0;
int.TryParse((string)Session["Employee"], out empId);
if (empId <= 0)
{
return RedirectToAction("IsAuth_Page", "Home");
}
return View();
}
[HttpPost]
public ActionResult ShowMyAtdByDate(string dateFrom, string dateTo)
{
if (dateFrom != "" && dateTo == "" && empId > 0)
{
...
}
else if (dateFrom == "" && dateTo != "" && empId > 0)
{
...
}
etc...
}
并确保在您的视图中您的表格具有方法post