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:
01559388
Vues:
58
This message has been marked as a message which has helped to the initial question of the thread.
You don't need to replace your Client class - the client class can just be a property of the ViewModel (although technically you're supposed to have everything that gets bound be individual props of the ViewModel, but I actually rarely do that myself).

In my case the ViewModel is a derived class - my internal ViewModel base includes things like ErrorMessage and an ErrorDisplayObject that can render an error message with formatting etc.

If you broke it down it would looks something like this:
public class EntryViewModel : BaseViewModel
{
     // The actual binding entity - doesn't have to be an entitiy
     // could also be individual values. There could be more than one here
     public Entry Entry {get; set; }
    
     // inherited from my BaseViewModel      
     public ErrorDisplay ErrorDisplay {get; set; }   // object that renders errors
     public string ErrorMessage {get; set; }  // single error messages
     public UserState UserState {get; set; }  // Logon information

     public int Id {get; set; }  
}
The point of the ViewModel is that it gives a top level object so you can bind multiple entities or any number of values that don't directly map to your business layer. You can also hang off things that the View might need - for special rendering operations, or for validations etc.

It's just a wrapper around your main object that you bind to.

+++ Rick ---

>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.
+++ 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
Répondre
Fil
Voir

Click here to load this message in the networking platform