if语句总是执行,控制问题

时间:2014-02-01 07:39:13

标签: asp.net .net asp.net-mvc c#-4.0 if-statement

我的代码中遇到了烦人的问题。我正在使用DATE FROM ---- DATE TO框来选择日期范围以在WEBGRID中显示该日期范围之间的员工但问题是在第一次显示时它完全根据该日期范围显示但是当我进行分页时(单击第2页) ,3,4 ......)然后它选择每个日期的所有员工记录,因为我已设置IF条件,如果DatFrom和TO为空,则选择所有结果

那么如何正确实现呢?

 public ActionResult ShowCalTextBox(String DateFrom, String DateTo) 
        {

            if (DateFrom != "" && DateTo == "") 
            {
                IEnumerable<GetEmpRec_DateResult> EmpRec_DateFrom = DataContext.GetEmpRec_Date(DateFrom, null).ToList();
                ViewBag.Dates = "Records for"+" "+ DateFrom ;
                return View(EmpRec_DateFrom);

            }
            else if (DateFrom == "" && DateTo != "") 
            {
                IEnumerable<GetEmpRec_DateResult> EmpRec_DateTo = DataContext.GetEmpRec_Date(null, DateTo).ToList();
                ViewBag.Dates = "Records for" + " " + DateTo;
                return View(EmpRec_DateTo);
            }
            else if (DateFrom != "" && DateTo != "") 
            {
                IEnumerable<GetEmpRec_DateResult> EmpRec_ByDate = DataContext.GetEmpRec_Date(DateFrom, DateTo).ToList();
                ViewBag.Dates = "Records from" + " " + DateFrom +" "+"to"+" "+DateTo;
                return View(EmpRec_ByDate);
            }
            else if (DateFrom == "" && DateTo == "")
            {
                IEnumerable<GetEmpRec_DateResult> EmpRec_Default = DataContext.GetEmpRec_Date(null, null).ToList();
                ViewBag.Dates = "No date selection";
                return View(EmpRec_Default);
            }

            return View();
        }
        public ActionResult About()
        {
            return View();
        }

实际上我觉得问题就在这里

public ActionResult ShowCalTextBox(String DateFrom, String DateTo) 
            {

                if (DateFrom != "" && DateTo == "") 
                {
                    IEnumerable<GetEmpRec_DateResult> EmpRec_DateFrom = DataContext.GetEmpRec_Date(DateFrom, null).ToList();
                    ViewBag.Dates = "Records for"+" "+ DateFrom ;
                    return View(EmpRec_DateFrom);

                }

查看:

@using EmployeeAttendance_app.Models
<div>
@using (Html.BeginForm("ShowCalTextBox", "Home", FormMethod.Post)) 
{
  <label id="lblFrom">From</label>
  <input type="text" id="TxtBoxFrom" name="DateFrom" />
  <label id="lblTo">To</label>
  <input type="text" id="TxtBoxTo" name="DateTo"  />
  <br />
  <br />
  <button type="submit" id="btnSubmitDate" name="btnSubmit">Submit</button>  
}
</div>

<div>
 <h4>@ViewBag.Dates</h4>
 <br />
 @{

     var grid = new WebGrid(ViewData.Model, rowsPerPage: 20, canPage: false);

  }

 <div id="AllEmpGrid_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>

</div>

1 个答案:

答案 0 :(得分:1)

原因是页面网格发出了get请求而不是post。可能你可以通过包含以下javascript代码来掌握它:

<script type="text/javascript">
     $(function () {
        $('th a, tfoot a').click(function () {
            $('form').attr('action', $(this).attr('href')).submit();
            return false;
        });
    });
</script>