Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing one application in asp.net mvc. I have tab panel on my view in which I am binding two jquery datatables using serverside pagination on each tab. I have custom search panels above each datatable. these search panels have form tag and one search button. Whenever I click on search button from first tab , I am getting values from
Request.Form.GetValues("draw").FirstOrDefault()
, But when I am clicking search button on second form I am not able to get draw, length and start values of datatable.

Here is my first tab search panel form
@using (Html.BeginForm("SearchFilter", "Store", FormMethod.Post))
                                      {

                                          <div class="row">
                                              <div class="col-lg-3">
                                                  <label>Search</label>
                                                  <div class="form-group">
                                                      @Html.TextBoxFor(model => model.SearchPanel.SearchText, new { @class = "form-control" })
                                                  </div>
                                              </div>
                                              <div class="col-lg-3">
                                                  <label>Brand</label>
                                                  <div class="form-group">
                                                      @Html.ListBoxFor(model => model.SearchPanel.BrandSelected, new SelectList(Model.SearchPanel.Brands, "Id", "Name"), new { htmlAttributes = new { @class = "form-control" } })
                                                  </div>
                                              </div>
                                              <div class="col-lg-4">
                                                  <label>City</label>
                                                  <div class="form-group">
                                                      @Html.ListBoxFor(model => model.SearchPanel.CitySelected, new SelectList(Model.SearchPanel.Cities, "Id", "Name"), new { @class = "form-control" })
                                                  </div>
                                              </div>

                                              <div class="col-lg-2">
                                                  <button id="btnSearch" type="submit" class="btn btn-info btnshadow">Search</button>
                                              </div>

                                          </div>
                                      }


and here is my secod tab seach panel

<div class="row">
    <div class="col-lg-3">
        <label>Search</label>
        <div class="form-group">
            @Html.TextBoxFor(model => model.SearchPanelMaster.SearchText,  new { @class = "form-control", id = "SearchTextMaster" })
        </div>
    </div>
    <div class="col-lg-3">
        <label>Brand</label>
        <div class="form-group">
            @Html.ListBoxFor(model => model.SearchPanelMaster.BrandSelected, new SelectList(Model.SearchPanelMaster.Brands, "Id", "Name"), new { @class = "form-control", id = "BrandSelectedMaster" })
        </div>
    </div>
    <div class="col-lg-4">
        <label>City</label>
        <div class="form-group">
            @Html.ListBoxFor(model => model.SearchPanelMaster.CitySelected, new SelectList(Model.SearchPanelMaster.Cities, "Id", "Name"), new { @class = "form-control", id = "CitySelectedMaster" })
        </div>
    </div>

    <div class="col-lg-2">
        <button id="btnSearchMaster" type="submit" class="btn btn-info btnshadow">Search</button>
    </div>

</div>
}

In controller I have,
[HttpPost]

       public ActionResult SearchFilter(StoreListViewModel model)
       {
           if (Session["AccessInfo"] == null)
           {
               return RedirectToAction("SessionTimeOut", "Home");
           }
           ViewBag.Tab = "";
           TempData["Query"] = model;
           TempData.Keep();

           model.SearchPanel.Cities = new SellerBl().GetCities();
           model.SearchPanel.Brands = new BrandBl().GetBrands(Constants.Status.Active);
           model.SearchPanelMaster.Cities = new SellerBl().GetCities();
           model.SearchPanelMaster.Brands = new BrandBl().GetBrands(Constants.Status.Active);
           ViewBag.Success = false;
           return View("StoreList", model);
       }
       public ActionResult SearchFilterMaster(StoreListViewModel model)
       {
           if (Session["AccessInfo"] == null)
           {
               return RedirectToAction("SessionTimeOut", "Home");
           }
           ViewBag.Tab = "";
           TempData["QueryMaster"] = model;
           TempData.Keep();

           model.SearchPanel.Cities = new SellerBl().GetCities();
           model.SearchPanel.Brands = new BrandBl().GetBrands(Constants.Status.Active);
           model.SearchPanelMaster.Cities = new SellerBl().GetCities();
           model.SearchPanelMaster.Brands = new BrandBl().GetBrands(Constants.Status.Active);
           ViewBag.Success = false;
           return View("StoreList", model);
       }

[HttpPost]
        public ActionResult LoadMasterData()
        {
            ViewBag.Tab = "";
            var draw = Request.Form.GetValues("draw").FirstOrDefault();
            var length = Request.Form.GetValues("length").FirstOrDefault();
            var start = Request.Form.GetValues("start").FirstOrDefault();
          //  string searchby = Request.Form.GetValues("search[value]").FirstOrDefault();

            int pageSize = length != null ? Convert.ToInt32(length) : 0;
            int skip = start != null ? Convert.ToInt16(start) : 0;
            int pageNo = skip == 0 ? 1 : (skip / pageSize) + 1;
            var stores = new EPagination<StoreViewModel>();
            var model = new StoreListViewModel();
            if (TempData["QueryMaster"] != null)
            {
                model = (StoreListViewModel)TempData["QueryMaster"];
            }
            ESearchRequest request = new ESearchRequest()
            {
                ItemCount = pageSize,
                Name = model.SearchPanelMaster.SearchText != null ? model.SearchPanelMaster.SearchText : "",
                PageNumber = pageNo,
                BrandIds = model.SearchPanelMaster.BrandSelected != null ? model.SearchPanelMaster.BrandSelected.ToList() : null,
                CityIds = model.SearchPanelMaster.CitySelected != null ? model.SearchPanelMaster.CitySelected.ToList() : null
            };
            try
            {

                UserProfileViewModel userProfile = (UserProfileViewModel)System.Web.HttpContext.Current.Session["UserProfile"];

                if (userProfile.Roles.Any(r => r.RoleName.Equals(Role.OperationAdmin)))
                {
                    stores = storeBl.GetStores(Constants.Status.Pending, request);
                }
                else if (userProfile.Roles.Any(r => r.RoleName.Equals(Role.Operation)))
                {
                    stores = storeBl.GetStores(Constants.Status.All, request);
                }
                else if (userProfile.Roles.Any(r => r.RoleName.Equals(Role.Viewer)))
                {
                    stores = storeBl.GetStores(Constants.Status.All, request);
                }
                var data = stores.PageData; //approvalStores.OrderBy(x => x.Status).Skip(skip).Take(pageSize);
                return Json(new { draw, recordsFiltered = stores.TotalRecords, recordsTotal = stores.TotalRecords, data }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

[HttpPost]
       public ActionResult LoadData()
       {
           ViewBag.Tab = "";
           var draw = Request.Form.GetValues("draw").FirstOrDefault();
           var length = Request.Form.GetValues("length").FirstOrDefault();
           var start = Request.Form.GetValues("start").FirstOrDefault();
          // string searchby = Request.Form.GetValues("search[value]").FirstOrDefault();

           int pageSize = length != null ? Convert.ToInt32(length) : 0;
           int skip = start != null ? Convert.ToInt16(start) : 0;
           int pageNo = skip == 0 ? 1 : (skip / pageSize) + 1;
           var approvalStores = new EPagination<ApprovalStoreViewModel>();
           var model = new StoreListViewModel();
           if (TempData["Query"] != null)
           {
               model = (StoreListViewModel)TempData["Query"];
           }
           ESearchRequest request = new ESearchRequest()
           {
               ItemCount = pageSize,
               Name = model.SearchPanel.SearchText !=null?  model.SearchPanel.SearchText : "",
               PageNumber = pageNo,
               BrandIds = model.SearchPanel.BrandSelected != null ? model.SearchPanel.BrandSelected.ToList() : null,
               CityIds = model.SearchPanel.CitySelected != null ? model.SearchPanel.CitySelected.ToList() : null
           };
           try
           {

               UserProfileViewModel userProfile = (UserProfileViewModel)System.Web.HttpContext.Current.Session["UserProfile"];

               if (userProfile.Roles.Any(r => r.RoleName.Equals(Role.OperationAdmin)))
               {
                   approvalStores = storeBl.GetApprovalStores(Constants.Status.Pending, request);
               }
               else if (userProfile.Roles.Any(r => r.RoleName.Equals(Role.Operation)))
               {
                   approvalStores = storeBl.GetApprovalStores(Constants.Status.All, request);
               }
               else if (userProfile.Roles.Any(r => r.RoleName.Equals(Role.Viewer)))
               {
                   approvalStores = storeBl.GetApprovalStores(Constants.Status.All, request);
               }
               var data = approvalStores.PageData; //approvalStores.OrderBy(x => x.Status).Skip(skip).Take(pageSize);
               return Json(new { draw, recordsFiltered = approvalStores.TotalRecords, recordsTotal = approvalStores.TotalRecords, data }, JsonRequestBehavior.AllowGet);
           }
           catch (Exception ex)
           {
               throw ex;
           }

       }


whenever I am clicking on second search panel search button I am not getting
var draw = Request.Form.GetValues("draw").FirstOrDefault();
          var length = Request.Form.GetValues("length").FirstOrDefault();
          var start = Request.Form.GetValues("start").FirstOrDefault();

these values

What I have tried:

I don't know why this is happening, should I remove tab panel or do something else
Posted
Updated 6-Aug-20 23:50pm

1 solution

The form you've shown doesn't contain fields for draw, length, or start.

The other form tag seems to be missing from your question.

But bear in mind that when you submit a form, only the fields contained within that form will be sent to the server. If you want the fields from both tabs to be sent at the same time, you need them to be within a single form.
 
Share this answer
 
Comments
Member 14908411 7-Aug-20 10:11am    
Hi Richard, I am binding jquery datatable on page load, and this form is for only search filter. The table which I am binding is outside of this form tag and draw, length and start are fields from this jquery datatable. Though it is outside the form still I am getting these fields on postback from first tab search button. But when I clicking second tab search it doesn't work.
$('#firstTabGrid').DataTable({
"processing": true,
"dom": 'lrtip',
"language": {
sLoadingRecords: ''
},
"serverSide": true,
"ajax": {
"url": '@Url.Action("LoadData", "Store")',
"type": "POST",
"datatype": "json"
},
"columnDefs": [
{
"targets": [ 0 ], //first column
"visible": @(userProfile.Roles.Any(r => r.RoleName.Equals(Role.OperationAdmin)) ? "true" : "false"),
'orderable': false
},
{
"targets": [ 22 ],
"cellClass": function (params) { return (params.value == 'Pending' ? 'badge-warning' : params.value == 'Approved' ? 'badge-success' :'badge-danger'); }

}
,
{
"targets": [ 23 ],
"visible": @(userProfile.Roles.Any(r => r.RoleName.Equals(Role.Operation)) ? "true" : "false")
}
,
{
'targets': [3, 6, 8, 10, 14, 16, 20, 22, 23, 24],
'createdCell': function (td, cellData, rowData, row, col) {
$(td).attr('id', 'noExl');
}
}

],
"columns": [
{
"data": function (source, type, val) {


return '';

}
},
{ "data": "Id", className: "hide"},
{ "data": "Name", },
{ "data": "BrandName" },
{ "data": "BrandName" ,className: "hide"},
{ "data": "Description" },

{ "data": "CityName"},
{ "data": "CityName", className: "hide"},
{ "data": "Address1" },
{ "data": "Address1", className: "hide" },
{ "data": "Address2"},
{ "data": "Address2", className: "hide" },
{ "data": "Longitude", className: "hide" },
{ "data": "Latit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900