asp.net-mvc


What is the right design pattern for custom template types in ASP.NET MVC?


Here's my situation: I've got a number of specialized object types in my application, and I'm following the standard convention for displaying them with custom templates in the /Shared/DisplayTemplates folder, and editing them with templates in /Shared/EditorTemplates. But I also want to be able to display a custom filter template for each type, so I'd like to add a /Shared/FilterTemplates folder, and implement my own #Html.FilterFor method, so that showing a Filter template is exactly like showing a Display or Editor template.
Does this seem like the best way to handle this situation, or is there a more correct/elegant way to do this in MVC? Thanks in advance.
I'm always using EditorTemplates when data is sent back to server. I assume the user can submit the filter to the server to perform the actual filtering.
When creating filters I prefer to create a model for the filter like:
public class UserListFilterModel
{
public string Username { get; set; }
public bool IsEnabled { get; set; }
}
The view for UserListFilterModel goes into EditorTemplates/UserListFilterModel.ascx.
And then add it as a property on my view model for the page.
public class MyPageViewModel
{
public UserListFilterModel Filter { get; set; }
}
Then I add the filter model to the model for the page and displays it like this:
<%= Html.EditorFor(x => x.Filter)%>
You are probably wrapping the filter in a form to allow the user to submit the values so I think it belongs in EditorTemplates. The users is in fact editing the filter model.
(If you really want to separate them ing you could use the UIHintAttribute but I wouldn't)
Edit: I added some sample code.
I think you misunderstand how Templates work. Templates do not make sense in the context you are describing.
Templates work on a SINGLE data item (although that data item can contain multiple data items, which in turn have their own templates).
The concept of a Filter is to control multiple data items, thus they do not map well to a template.
What you could do is create a DisplayTemplate for your collection class that adds filtering, thus no need to create a custom type of template. Just use DisplayTemplates.

Related Links

How to Pass QueryString in MVC WEBAPI routing for the following URL
How to save multiple files to database rather than disk
In MVC can ViewModels access service layer?
Telerik mvc grid, columns.bound to dictionary value. or “dynamic property in model”
RouteLink to an API controller not working in MVC 4 with areas
Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5 [duplicate]
what is the best solution of accessing action method in another controller asp.mvc?
Using Swashbuckle to Host an Existing Swagger.JSON file
Http Request Timeouts on Azure Web Apps
Autofac + OWIN + Web Api ASP.NET MVC constructor injection not working
.NET MVC controllers and API controllers in the same project
How to send email to registered user (MVC)
RowAction styles lost while sorting Telerik extension grid
Nuget Requirement Conflict
how Use ActionLink in Folder App_Code Mvc6 Asp.Net
MVC application Performance

Categories

HOME
demandware
logstash
watson
android-wear
styling
passwords
libreoffice
ionic3
coded-ui-tests
ceph
apache-spark-sql
pie-chart
uwsgi
chart.js2
jscrollpane
condor
plots.jl
installer
monaco-editor
data-annotations
lazy-loading
abaqus
procedure
jpos
cayley
opencms
many-to-many
rets
typemock-isolator
point-cloud-library
editorconfig
mobile-center
scom
machine-language
groove
server-sent-events
delete-row
extraction
partition
rft
word-embedding
jpype
gradle-tooling-api
spark-submit
zuul
html-form
jquery-masonry
canopen
fasta
python-module
untagged
gpt
blocking
sscanf
mailcore2
angular2-rc5
setwindowshookex
heritrix
webpagetest
telecommunication
dms
mongolab
rabbitvcs
vimeo-ios
ane
dnx
interpreted-language
sample-data
android-gpuimageview
heroku-toolbelt
user-registration
spinach
viennacl
android-print-framework
vendor
swift-array
textselection
b2b
data-dictionary
execcommand
ioerror
sugar.js
kmz
datadesign
qbwc
android-actionbar-compat
51degrees
nextpeer
ria
sticky-footer
cellphone
ed
android-screen
bapi
blackberry-jde
openfeint
jdic
servletunit
spquery
arraycopy
network-scan
drupal-gmap
sharepoint-feature
roguelike

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App