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

Securing the Forms Authentication Cookie when offloading SSL
Asp.Net MVC - FluentValidation
How to handle callbacks in asp.net?
Authorization in a multi-homed environment
MVC Views from Database
How can I access the HttpServerUtility instance in ASP.NET MVC controller?
Can you map the results of a manual SQL query to objects in Entity Framework?
Any way to detect classic and integrated application pool in code?
ASP.Net MVC 2 authentication (login) best practices
overload ActionResult
Razor: Creating a view from a nested layout doesn't auto create the sections
Saving entities with foreign key in ASP.NET MVC
“HTTP Error 404.3 - Not Found” force download of .step file in IIS7.5
MVC3 EF4 POCO Repository/UnitOfWork Connection Error
CanvasAutorize - redirection to facebook permissions dialog clears the request_ids query string sent when user accepts an app request
Unable to set membernames from custom validation attribute in MVC2

Categories

HOME
ssis
vb.net
hadoop
lsf
redirect
gwt
coffeescript
varnish
android-activity
watson
android-sms
oracle-adf
google-analytics-api
telnet
calayer
librsvg
wavelet
proftpd
google-cloud-sdk
raspberry-pi2
watchservice
eigen3
teraterm
jscodeshift
jmeter-plugins
broker
ratchet-2
epicorerp
azure-redis-cache
angular-services
kprobe
rdf4j
clappr
whatsapi
jsplumb
toastr
directx-12
nest-thermostat
pyparsing
blockui
lifecycle
google-crawlers
sieve-of-eratosthenes
phing
titanium-alloy
folly
xaf
bits
disqus
ipod-touch
logicblox
project-centennial
invalidate
uifont
notimplementedexception
recurrence
jasmine2.0
node.js-tape
derbyjs
jawr
billing
android-camera-intent
mapr
callkit
lapply
angular2-rc5
proc
jsbin
post-increment
webclient-download
4d
enhanced-ecommerce
oracle-policy-automation
cover
cucumber-java
clipping
delphi-10-seattle
symbian
ubiquity
many-to-one
julian
treegrid
error-log
structured-programming
android-print-framework
size-t
isql
servlet-listeners
mpj-express
laravel-validation
gdk
iqueryable
publish-actions
before-save
twill
jqgrid-php
modalpopupextender
keymapping
hla
sizzle
icon-fonts
layout-manager
haar-wavelet
rc-shell
documentviewer
working-directory
sticky-footer
bapi
mdi
facebook-authentication
having
treetop
alivepdf
system-tray
custom-protocol
drupal-gmap
pinging
utility
iphone-sdk-3.1
clipboard-pictures
stsadm

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