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

Display rows in MVC .NET from joining tables
Do not display results till search is executed in MVC
WebApi 2 Route not resolving with guid as parameter
mvc filter by dropdown selected text
Windows Authentication together with Asp.net MVC SimpleMembership?
Jquery validations stops working for an input when using masked plugin
set textarea text from ViewData asp.net mvc
Azure download blob filestream/memorystream
Bootstrap 3.1.1 less in ASP.NET MVC 5 have error with EnableOptimizations in true
simple membership provider with active directory
Lambda expression for getting a table row and update the row in database
How to call a HttpPost method from Controller in ASP.net MVC 4?
How do you escape a '#' symbol within in a url with razor
ASP MVC5 list/edit/view users
anchor tag in MVC partial view doesnt work as expected
OData filter not working for my Web API (mvc 4 stack)

Categories

HOME
clojure
music
compiler-errors
watson
spring-roo
passwords
turing
openvpn
jasypt
android-dialer
webdriver-io
socrata
base
boxfuse
pymc3
lag
nsmutableattributedstring
textmatebundles
advantage-database-server
pwm
netcdf
jcrop
core-location
naturallyspeaking
structure
desktop-app-converter
simple-html-dom
plm
codeceptjs
pyparsing
django-users
dokuwiki
petrel
server-sent-events
hamcrest
chamilo-lms
data-science
amazon-vpc
mindstorms
uitabbarcontroller
angular2-seed
kendo-chart
dependency-management
ms-jet-ace
ssms-2012
asyncsocket
federation
jasmine2.0
koala-framework
view-source
jawr
waf
lapply
kombu
setwindowshookex
coldfusion-11
xnamespace
heritrix
kernighan-and-ritchie
android-number-picker
winobjc
fab
aldryn
materialdrawer
actioncable
linq2db
python-2.3
heroku-toolbelt
avalonedit
selectsinglenode
treegrid
isql
imake
gnat
dynamic-data-display
nhunspell
bson
dfsort
array-address
vlfeat
transport-stream
nsusernotificationcenter
12factor
google-provisioning-api
rgba
expressionvisitor
ondraw
livevalidation
mate
demoscene
eye-detection
hunchentoot
custom-tag
rails-3.1
custom-protocol
xml-database
net-ssh
palm-pre
templatebinding
web-based
xlink

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