Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Saving data
Message
 
À
11/12/2012 18:24:29
Information générale
Forum:
ASP.NET
Catégorie:
MVC
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01559354
Message ID:
01559387
Vues:
61
This message has been marked as a message which has helped to the initial question of the thread.
Yes and no. Modelbinding validation only takes you so far. If you have dynamic rules then model based validation falls apart.

I do use the validation attributes on the entity model, but only for basic stuff like empty values, ranges etc.. Ultimately, I want validation logic in my business object that can check it all. My business layer looks at validation errors that are internally assigned (via an OnValidate() method that allow for easy coded rules) or those that are fired via validation errors on the entity model when the model is saved by EF, so it works either way.

Modelbinding by itself is not enough IMHO...

+++ Rick ---



>Looks like you're rolling your own validation and display rather than using the standard ModelState.IsValid and ModelState.AddModelError(). Am I correct in this? If so, why did you roll your own?
>
>
>>You should use ModelBinding. Modelbinding maps fields of the form to an object with properties of the same name. Typically a view model is used rather than the entity so you can pass multiple pieces of information to the view - like error information or other messages that need to display in the UI.
>>
>>Here's an example of a model passed to save into an entity (using a bus object, but the same would roughly work with your EF repository):
>>
>>
>>        [HttpPost]
>>        public ActionResult Save(EntryViewModel model)
>>        {
>>            if (!ModelState.IsValid)
>>            {
>>                // Handle error and display error result
>>                model.ErrorDisplay.ShowError(model.ErrorDisplay.AddMessages(ModelState))
>>                return View("Show");
>>            }
>>
>>            var entryBus = new busEntry();
>>
>>            // get entity that needs to be updated
>>            var entry = entryBus.Entity;
>>            entry.Id = model.entry.Id;
>>            entry.Title = model.entry.Title;
>>            entry.Description = model.entry.Description;
>>            // ... more fields to map
>>
>>            // Or you can attach the entity - in my bus object
>>            // this is automatic, with EF you can use DbSet::Attach
>>            //entryBus.Attach(model.entry);
>>
>>            // Validate and save the bus object
>>            if (!entryBus.Validate() || !entryBus.Save())
>>            {
>>                model.ErrorDisplay.ShowError(model.ErrorDisplay.AddMessages(entryBus.ValidationErrors));
>>                return View("Show");
>>            }
>>
>>            // Saved and done
>>            return View("Index");
>>        }
>>
>>
>>Hope this helps,
>>
>>+++ Rick ---
>>
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform