Is there a way to have the same kind of client-side validation in MVC 3 without being able to set the validation at the viewmodel level?
I have two requirements that are preventing me from adding my validation at the viewmodel level. I'd like to be able to have the same kind of "built-in" validation but without changing the viewmodel or controller code. I'd like to be able to change, turn on/off, validation at the view level post-deployment. Ideally I'd like to be able to edit the view mark-up without writing and maintaining mountains of my own JavaScript.
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
This may help ya, only validation on the client though so not really ideal.
You could create your own TypeDescriptionProvider which will allow to augment your viewmodel at runtime.
Based on this code sample (XmlMetadataTypeDescriptor, XmlMetadataTypeDescriptionProvider) you should be able to:
Augment viewmodel using external xml which contains validation attributes.
Turn on validation with
TypeDescriptor.AddProvider
and turn it off with:
TypeDescriptor.RemoveProvider
Update
For more information you can read great article: "Understanding the TypeDescriptor: A Metadata Engine for Designtime Code".
Related
I am implementing various analytic tracking services in an MVC3 site (Google, Coremetrics) and researching if a custom HTML helper or partial view would work better given the following details:
-Code runs multiple sites and business logic is needed to change analytic service account Ids.
-The class must accept an object with order details to render the appropriate tags.
-The code must know which view is being rendered.
The solution I am working on includes am HTML helper base class that accepts the needed objects and is inherited by each provider's individual HTML helper. These helpers will live on the common layout. Is there a better way of implementing analytics on MVC and are partial views better suited since business logic is needed?
The GA analytics tracking code is pretty simple, we also use ASP.NET MVC3 but have just dropped the necessary code directly into our 'Order Complete' view.
There's no reason why you couldn't use an HTML helper instead though. I suggest not overly complicating your object model. Just use a single helper that has parameters matching the parameters used in the JavaScript.
You should be able to find examples of eCommerce tracking code, add parameters as appropriate, done. You might want to create another overload that pulls GA ID and domain name from the web.config as in the example as well, but still accepts the order details as parameters.
I am trying to find the way to build complex web pages with MVC3 and AJAX.
I would like to use components to achieve this.
Each component is consisted of it's own model, view and controller.
Multiple components are then placed on some complex view and should act together to
provide desired behaviors.
In some situations, when user performs some action (interaction) with one of the components,
I must update other portions of the page via AJAX.
Component on which action (interaction) occurred, in it's implementation, does not assume anything about view on which it will be used and what portions of the pages should be updated and how.
So when some interaction occurs in some component, I need a mechanism (outside component itself) which will handle this situation and update appropriate parts of the page.
How would you, generally, implement such mechanism?
I would use the Mediator Pattern, also sometimes mistakenly referred to as the manager pattern.
This class would mediate the communication of your components.
Right now the application being built by our team uses the built in MVC attributes and a few home baked ones to validate the View Models. Because of best practice design principles, we have placed those same rules in the Logical Layer. This has unfortunately caused duplication of validation code.
In MVC3 at least, if JavaScript is disabled, these same attributes will still perform the validation they are meant to, so transforming a View Model in to a DTO and asking the Logical Layer to validate it is not an option because this process would have already been done by the framework.
I have not found the following SO post to be of any help. I have used MS Enterprise Library and the API did not sit well with our team.
Good practices for avoiding validation logic duplication when working with both domain objects and view models in ASP.NET MVC
I'm thinking that the best way to do this is to have the validation attributes bound at runtime to specific properties and have a dependency injection container do this. Is this possible or is there a different approach we could take?
You are asking for multiple validation types to be performed here.
You want client validation (it seems) and some other business validation layer.
If thats the case the only choices as I see it are:
duplicate the code (ya I know Im listing options)
1a. use data annotations on your objects for client validation. Business layer validation happens however you define, and separate as a final check. If you use for instance the entity framework's fluent API this is a standard route.
client side validation is just that - helpers for client side. domain validation will happen upon save. This is ideally the more powerful approach, but isn't as friendly when your domain objects don't match property names on your view models, so you need to map domain errors to view model property errors which normally isnt too bad but can get iffy.
implement IValidteableObject (and rid of client validation). This validation logic is then called from your logical layer and the model binder.
You can inject code for validation, but its not pretty and Im not sure how reusable it is outside the MVC validation route.
There may be other ways beyond this, but those are the main options as I see it.
One way of seeing it is: If your api is your mvc application and the validation is already done by the point you are executing your business rules, you can just assume they are valid. It's input validation, of course is something related to your domain but... if you change the way of thinking, your domain expects valid data and assumes you did your homework before using the business rules.
What is the best way to do validation in MVC 3? Here are the requirements:
Works client and server side.
Shares as much code between client and server as possible (attribute on model property seems ideal)
Works across async request
Display errors, validation messages, and success messages coming from the server side
Unobtrusive javascript, as minimal as possible
Dynamically added HTML should still validate the same way
My task this weekend is to build a robust solution for this, figured I'd ask here first before re-inventing or re-discovering the wheel with blood sweat and tears.
I would check out Brad Wilson's blog on this. He covers using unobtrusive validation in MVC3, sounds like exactly what you're looking for.
Adding more info per OP's comment
Regarding server side validation (custom validation), check out #jfar's response to a similar question I posted regarding custom validation -- he suggests that you should question your design if you're relying heavily on custom validation. In my case, I ended up going either with Ajax to handle my custom validation, or allowed the postback to perform the validation.
I want to implement filtering in my jqGrid in my Spring Framework MVC 3.0 project. Where can I find sample code or documentation telling how to use filtering in this?
I just posted an answer on the same question (probably from you) on http://www.trirand.com/blog/?page_id=393/help/jqgrid-filtering. To make other people easy to read the answer I post the same information here.
The answer on your question depends a little how you interpret the "filtering". If you want use some external controls (selects, checkboxes and so on on the same page where you have jqGrid) to filter your data I recommend you to read How to filter the jqGrid data NOT using the built in search/filter box.
If you want to implement data filtering inside of jqGrid you can choose between serching with respect of "search" and reset of serching results with respect of "refresh" buttons of the navigator (simple searching and advanced searching), the usage of toolbar searching (conside the usage of stringResult: true to be more conform with other form of searching) and custom searching.
If you decide to use advance searching you can just add string filter parameter to the (see Guidance on a better way to retain filtering options when using ASP.NET MVC 2) action which provide the grid data or add three string parameters searchField, searchString, searchOper if you want to use simple searching feature. In all cases you will have to add WHERE to the SELECT statments which will be constructed in your program based on the values of the new parameters.
Probably other people post you more URLs to the good full code examples which you could use.
UPATED: I don't use Spring Framework myself, so I could not help you with any Spring MVC 3.0 examples. Some general solution is more common. For example in ASP.NET MVC solutions I prefer don't fill the data in the grid directly using MVC. Instead of that I provide a JSON web service (implemented as a part of ASP.NET MVC solutions or as WCFservice which are the part of the same web site) which URL I place in the jqGrid parameter. So you should just invest in the writing on business logic in form of JSON/XML web service which provide the data. The most jqGrid specific code you can write in JavaScript. So you can share the experience of other people in the usage of jqGrid and have a clear separation of HTML code from the business logic. Moreover JSON/XML web services can be better tested for example with respect of unit tests.