Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Saving data
Message
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:
01559363
Vues:
42
Can you show your EntryViewModel class also, please, so I may try to figure out how to replace my Client class with the ClientViewModel.

I'll take a look at the Attach method because setting properties manually seems like a lot of work and also you should know all the properties of the class.


>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 ---
>
>>Hi everybody,
>>
>>I am a bit uncertain of the right way of saving data with ASP.NET MVC.
>>
>>I have 2 extra projects - Objects that defines my models (say, Client class), Data that defines my repository.
>>
>>In my Controller I currently have in the Using top of the file:
>>
>>
>>using System;
>>using System.Linq;
>>using System.Web.Mvc;
>>using CardNumbers.Models;
>>using CardNumbers.Data;
>>
>>namespace CardNumbers.Controllers
>>{
>>    public class ClientController : Controller
>>    {
>>        private IClientRepository Db;
>>
>>        public ClientController()
>>        {
>>            this.Db = new ClientRepository(new CardNumbersContext());
>>        }
>>        public ClientController(IClientRepository Db)
>>        {
>>            this.Db = Db;
>>        }
>>
>>And this is what I currently have in the post version of the Edit method:
>>
>>
>> [HttpPost]
>>        public ActionResult Edit(int id, FormCollection collection)
>>        {
>>            try
>>            {
>>                var client = Db.GetClientById(id);
>>
>>                client.Address = collection["Address"];
>>                return this.Client(collection);
>>            }
>>            catch
>>            {
>>                return View();
>>            }
>>        }
>>
>>I don't think this is what I really want for the Edit - I need to somehow update my client and I don't actually want to map each property manually using FormCollection.
>>
>>Do you know what is the right way here to update the client object? Should I bring reference to the CardNumbers.Objects into this code also and instead of using FormCollection attempt to use Client _client
>>?
>>
>>Can you please show me how this is handled?
>>
>>Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform