I Lost filter data with mvc paging while i filtered with viewmodel not text

Here is my controller:

public ActionResult Index(ServiceRequestViewModel modelSearch, int? ServiesRequestIDByRadiobtn, int? Page)
{
    ViewBag.modelreqtype = modelSearch;
    var ModelQuery = from a in _db.V_Service_Request_List
                        where ((string.IsNullOrEmpty(modelSearch.ServiceRequestNumber) ? true : a.ServiceRequestNumber.Contains(modelSearch.ServiceRequestNumber)) &&
                                (!modelSearch.RequestTypeCode.HasValue ? true : a.RequestTypeCode == modelSearch.RequestTypeCode) &&
                                (!modelSearch.RequestSubTypeCode.HasValue ? true : a.RequestSubTypeCode == modelSearch.RequestSubTypeCode) &&
                                (string.IsNullOrEmpty(modelSearch.PlantName) ? true : a.PlantNameAR.Contains(modelSearch.PlantName) || a.PlantNameEn.Contains(modelSearch.PlantName)) &&
                                (!modelSearch.RegionId.HasValue ? true : a.RegionId == modelSearch.RegionId) &&
                                (string.IsNullOrEmpty(modelSearch.IndustryLicenseNumber) ? true : a.IndustryLicenseNumber.Contains(modelSearch.IndustryLicenseNumber)) &&
                                (!modelSearch.StatusCode.HasValue ? true : a.StatusCode.Value == modelSearch.StatusCode) &&
                                (!modelSearch.ServiceRequestFromDate.HasValue ? true : a.ServiceRequestCreatedDate >= modelSearch.ServiceRequestFromDate) &&
                                (!modelSearch.ServiceRequestToDate.HasValue ? true : a.ServiceRequestCreatedDate <= modelSearch.ServiceRequestToDate))
                        select a;
    
    int RequestTypesCode = GetRequestTypesIDFromUserRole();

    var assignedTasks = (from a in DB.V_Service_Request_List
                            where a.AssigneeId == CurrentUserId
                            select a).ToList();

    //----------------------- Get Evaluator data For DRP------------------------------------------------
    ViewBag.EvaluatorsModel = GetEvaluatorsData();
    //----------------------------------------------------------------------
    var model = new ServiceRequestViewModel()
    {
        Regions = _db.Regions.ToList(),  //---المنطقة
        List_RequestSubTypes = modelSearch.RequestTypeCode.HasValue ? _db.Request_Sub_Type.Where(a => modelSearch.RequestTypeCode.HasValue ? a.request_type_code == modelSearch.RequestTypeCode : true).ToList() : new List<Request_Sub_Type>(), // نوع الاجراء
        List_RequestTypeStatuses = modelSearch.RequestTypeCode.HasValue ? _db.Request_Type_Status.Where(a => modelSearch.RequestTypeCode.HasValue ? a.request_type_code == modelSearch.RequestTypeCode : true).ToList() : new List<Request_Type_Status>(),
        //var userlist = _db.AspNetUsers.Where(a => a.AspNetRoles.Any(x => btnlist.Contains(x.Id)));
    };

    model.List_ServiceRequestTable = model.List_ServiceRequestTable.Where(x => x.StatusCode != IL_Draft)
        .OrderByDescending(x => x.ServiceRequestCreatedDate).ToPagedList(Pagenumber, PageSize);
    return View(model);
}

and HTML like this:

@Html.PagedListPager(Model, page => Url.Action("Index", new { modelSearch = ViewBag.modelSearch, page }));

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I solved it with myself :
and I did that in my controller to save my filter data with server-side paging

   public ActionResult Index(ServiceRequestViewModel modelSearch, int? ServiesRequestIDByRadiobtn, int? Page)
    {

        #region Pagination Conditions
        var TestModel = modelSearch;
        if (modelSearch.RequestTypeCode != null|| modelSearch.IndustryLicenseNumber != null || modelSearch.PlantName != null || modelSearch.RegionId != null || modelSearch.RequestSubTypeCode != null || modelSearch.ServiceRequestFromDate != null || modelSearch.ServiceRequestNumber != null || modelSearch.ServiceRequestToDate != null || modelSearch.StatusCode != null)
        {
            TempData["modelSearch"] = modelSearch;
        }
        else
        {
            if (Page != null)
            {
                TestModel = (ServiceRequestViewModel)TempData["modelSearch"];
                TempData["modelSearch"] = TestModel;
            }
            else
            {
                TempData["modelSearch"] = null;
            }

        }
        if (TestModel != null)
        {
            modelSearch = TestModel;
        } 
        #endregion

        var ModelQuery = from a in _db.V_Service_Request_List
                         where ((string.IsNullOrEmpty(modelSearch.ServiceRequestNumber) ? true : a.ServiceRequestNumber.Contains(modelSearch.ServiceRequestNumber)) &&
                                 (!modelSearch.RequestTypeCode.HasValue ? true : a.RequestTypeCode == modelSearch.RequestTypeCode) &&
                                 (!modelSearch.RequestSubTypeCode.HasValue ? true : a.RequestSubTypeCode == modelSearch.RequestSubTypeCode) &&
                                 (string.IsNullOrEmpty(modelSearch.PlantName) ? true : a.PlantNameAR.Contains(modelSearch.PlantName) || a.PlantNameEn.Contains(modelSearch.PlantName)) &&
                                 (!modelSearch.RegionId.HasValue ? true : a.RegionId == modelSearch.RegionId) &&
                                 (string.IsNullOrEmpty(modelSearch.IndustryLicenseNumber) ? true : a.IndustryLicenseNumber.Contains(modelSearch.IndustryLicenseNumber)) &&
                                 (!modelSearch.StatusCode.HasValue ? true : a.StatusCode.Value == modelSearch.StatusCode) &&
                                 (!modelSearch.ServiceRequestFromDate.HasValue ? true : a.ServiceRequestCreatedDate >= modelSearch.ServiceRequestFromDate) &&
                                 (!modelSearch.ServiceRequestToDate.HasValue ? true : a.ServiceRequestCreatedDate <= modelSearch.ServiceRequestToDate))
                         select a;


        int RequestTypesCode = GetRequestTypesIDFromUserRole();


        var assignedTasks = (from a in DB.V_Service_Request_List
                             where a.AssigneeId == CurrentUserId
                             select a).ToList();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x