Proper way to display form with filtering in ASP.NET MVC 5
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 userHistorySearchResult
thats view model for table contentHistorySearchCriteria
contains filtering options in formHistorySearchBusinessLogic.GetHistories(HistorySearchCriteria
I am using this to get data from database but with
searchModel)
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
requestTempData["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.
c# asp.net
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
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 userHistorySearchResult
thats view model for table contentHistorySearchCriteria
contains filtering options in formHistorySearchBusinessLogic.GetHistories(HistorySearchCriteria
I am using this to get data from database but with
searchModel)
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
requestTempData["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.
c# asp.net
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
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 userHistorySearchResult
thats view model for table contentHistorySearchCriteria
contains filtering options in formHistorySearchBusinessLogic.GetHistories(HistorySearchCriteria
I am using this to get data from database but with
searchModel)
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
requestTempData["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.
c# asp.net
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 userHistorySearchResult
thats view model for table contentHistorySearchCriteria
contains filtering options in formHistorySearchBusinessLogic.GetHistories(HistorySearchCriteria
I am using this to get data from database but with
searchModel)
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
requestTempData["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.
c# asp.net
c# asp.net
asked Jun 25 at 13:13
dafie
213
213
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jun 29 at 14:35
Marc-Andre
6,24643161
6,24643161
answered Jun 29 at 11:52
Rud
1
1
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown