asp.net mvc - When to use a ViewModel rather than a Model? -


I have a business model named Customer, which has many essential properties (via dataetnotation) and other validation rules.

I have a view which means allowing editing of a customer's address field.

The problem I have is that I want a strongly typed view, but I type customer here here. Since the scene will only edit the address data because it will not return any of the other required data which will require the customer object to validate.

This suggests that I should use a Viomodalal, however, there are many business rules that apply to the customer's address related properties, which I would like to get new view (address length, zipcode, state formatting, Etc.) to duplicate. They require duplicate because client-side verification (I'm using xVal) requires that information to work.

I think that I have reached the catch-22 scenario. Soli tells me that I should not copy on a view model, which is already in my model but on the other hand I Can not use because it will never be valid.

What is the best practice in this situation?

Selected path

The solution which I was finally chosen needed to work in order to obtain the ViewModel path validity It was not just any other practical way.

However, they were unable to eliminate some obsolete places using Viudodalas. I reconstruct some models to use the interface, which contains the properties that I knew they used to see again in models. Since ViewModels can now use the same interface as the model allowed me to work in this way:

  Edit public actionl (see addressmodel address) {if (ModelState ISIID) return see! ; Var customer = customer.load (address.CustomerId); UpdateModel & LT; IAddress & gt; (Customer); // more stuff ....}  

This saves me the stage of using an autopart.

I answered the answer below (by Wyaut Barnett) that it is good for most cases and I use it on other projects, especially with Linq-to-Sql.

"post-text" itemprop = "text">

I went to the same issue with complex model squares which are not playing well with simple ideas and model binding. I was also used to use xVal. The move I used to have used to cover the dry angle for basic verification, then use the automaker to return things to full-fledged model classrooms. . Then I can run another round of server-side verification to cover more complex bits, which requires access to the database and so on.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -