Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Saving data
Message
De
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:
01559365
Vues:
44
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 ---
>
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform