Proper way to display form with filtering in ASP.NET MVC 5

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












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.







share|improve this question

























    up vote
    3
    down vote

    favorite












    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.







    share|improve this question





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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.







      share|improve this question











      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.









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jun 25 at 13:13









      dafie

      162




      162




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          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.






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197207%2fproper-way-to-display-form-with-filtering-in-asp-net-mvc-5%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            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.






            share|improve this answer



























              up vote
              0
              down vote













              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.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                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.






                share|improve this answer















                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.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jun 29 at 14:35









                Marc-Andre

                6,20143161




                6,20143161











                answered Jun 29 at 11:52









                Rud

                1




                1






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197207%2fproper-way-to-display-form-with-filtering-in-asp-net-mvc-5%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation