简体   繁体   中英

Get working but post not working

I was stuck by an annoying problem in my code but solved but confused me. I am using DATE FROM ---- DATE TO boxes to select range of date to display employees between that date ranges in WEBGRID but problem was that in first display it appears exactly according to that date ranges but when i do pagination (clicking page 2, 3 , 4....) then it picked all employees record for every date because i have set IF condition like that if DatFrom and TO are empty then pick all results like i mentioned in IF block. It all was due to FORMMETHOD.POST , when i wrote FORMMETHOD.GET then it started working correctly, WHY ?

so how to implement it correctly ?

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();
        }

VIEW:

@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>

The reason why your code doesn't work is very simple. You have an html <form> in which you have placed the start and end date input fields. The grid on the other hand is completely separate. If you use POST, then the values entered in those textboxes are sent in the body of the request and are not seen in the current url. So when the WebGrid renders, it cannot generate correct values for the pagination links. The reason for this is because when it generates those links it includes all query string parameters that are part of the current request url.

On the other hand, when you switch your form to use GET verb, then the values of the textboxes become part of the current url as query string parameters and when the WebGrid renders the pagination links it picks them up. That's the proper way to implement this:

@using (Html.BeginForm("ShowCalTextBox", "Home", FormMethod.Get))
{
    ...
}

If you use POST, the WebGrid has absolutely no way of knowing that you have some other html <form> in your page and pick cannot pick the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM