6
\$\begingroup\$

I want to create page when user can use form to display filtered data in table. Form has to remember its own state, because after every refresh it shouldnt change.

So after submitting form I should get:

  • form values
  • results from database based on what was checked in form

Seems pretty easy.

  • HistoryViewModel thats all data I want to display to user

  • HistorySearchResult thats view model for table content

  • HistorySearchCriteria contains filtering options in form

  • HistorySearchBusinessLogic.GetHistories(HistorySearchCriteria searchModel) I am using this to get data from database but with filtering applied.

At first I created my ViewModel which will holds data to display:

public class HistoryViewModel
{
    // form's filtering options here
    public HistorySearchCriteria HistorySearchCriteria { get; set; }

    // results after filtering
    public List<HistorySearchResult> HistorySearchResults { get; set; }

    // entries amount (needed for pagination)
    public int AmountOfEntries { get; set; }

    // avaible actions to modify row's data. For example on particular page only `delete` and `edit` will be possible
    public List<SiteEnums.GroupActionOptions> GroupActions { get; set; }
}

Now, let me give you a quick peek of HistorySearchCriteria. I am using this to get values from filtering form.

public class HistorySearchCriteria
{
    [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? DateFrom { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? DateTo { get; set; }

    public int? AmountFrom { get; set; }
    public int? AmountTo { get; set; }

    public string Title { get; set; }

    public List<SelectListItem> Banks { get; set; }
    public string SelectedBankId { get; set; }

    public List<SelectListItem> Statuses { get; set; }
    public int? SelectedStatusId { get; set; }

    public SiteEnums.ShowOptions Display { get; set; }
    public int? SelectedDisplayId { get; set; }
}

After making POST I am retreiving data from filtering form and using that data to fill another ViewModel:

[HttpPost]
public ActionResult Index(HistoryViewModel searchViewModel, int page = 0, int resultsAmount = 100)
{
    var logic = new HistorySearchBusinessLogic();

    // get data based on filtering options
    var query = logic.GetHistories(searchViewModel.HistorySearchCriteria);

    // do some validation
    if (page < 0) page = 0;
    if (resultsAmount < 0) resultsAmount = 100;

    // get data amount
    var amount = query.Count();

    // get ONLY important data
    var results = query.
        OrderBy(x => x.Id)
        .Skip(page * resultsAmount)
        .Take(resultsAmount)
        .Select(x => new HistorySearchResult
        {
            Amount = ((double)x.Amount / 100).ToString(),
            Bank = x.Bankaccount.BankShort.BankShortName,
            CustomerData = x.CustomerData,
            OperationDate = x.DateIncoming,
            SaveDate = x.DateExecutive,
            Title = x.Title
        })
        .ToList();

    // fill view model
    var viewModel = new HistoryViewModel()
    {
        HistorySearchCriteria = searchViewModel.HistorySearchCriteria,
        HistorySearchResults = results,
        GroupActions = new List<SiteEnums.GroupActionOptions>()
    };

    // pass data with RPG design pattern
    TempData["viewModelFromPost"] = viewModel;
    return RedirectToAction("Index");
}

Now in TempData["viewModelFromPost"] I should have data which holds my filtering criteria, results etc.

In GET I have 2 scenarions:

  • TempData["viewModelFromPost"] can be null, because thats my first request TempData["viewModelFromPost"] can contains data results, filtering criteria etc.

Lets see whats inside GET

[HttpGet]
public ActionResult Index()
{
    HistoryViewModel vm = new HistoryViewModel();

    if (TempData["viewModelFromPost"] != null)
    {
        vm = TempData["viewModelFromPost"] as HistoryViewModel;
    }
    else
    {
        vm.HistorySearchCriteria = new HistorySearchCriteria()
        {
            DateFrom = DateTime.Now,
            DateTo = DateTime.Now,
        };

        vm.HistorySearchResults = new List<HistorySearchResult>();
    }

    vm.GroupActions = new List<SiteEnums.GroupActionOptions>();

    vm.HistorySearchCriteria.Banks = _context.BankShortTypes.Select(x => new SelectListItem
    {
        Text = x.BankShortName,
        Value = x.Id.ToString()
    }).ToList();

    vm.HistorySearchCriteria.Statuses = _context.StatusTypes.Select(x => new SelectListItem
    {
        Text = x.StatusName,
        Value = x.Id.ToString()
    }).ToList();


    return View(vm);
}

Well, its complicated a bit. I think controllers should be lighter. What do you think about that? I think that could be archived much, much easier. It starting to be very complicated in GET, because I have to remeber which List I have to initialie, which data I have to get from database etc.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I think that pipeline pattern for filtering in connection with GET request will be a good way to go.
See details over here: Pipeline pattern for .Net

Just separate filtering as a separate service and pass around here checked by users params to filter.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.