How do you ignore / save values ​​in MVC when your view model does not have as many fields as your domain model?

I have a website where I use fluentNhibernate and Asp.net MVC. I have an editing view that allows the user to edit 8 out of 10 properties for this record (object). When you submit the form and model bindings, two non-editable fields are returned to the view model as empty lines or as the default DateTime values, depending on the type of property.

Since I also use AutoMapper to map my view model to my domain entity, I cannot just download a new copy of my object from the database and manually set two missing properties. What is the best way to save those fields that I don't want to edit?

One way to work is to store values ​​in hidden input fields in my view. It works, but it seems rude. I appreciate any advice. Is there a way in AutoMapper to configure this desired feature?

UPDATE: So, I think I am not trying to ignore the fields, I am trying to make sure that I do not store null or empty row values. Ignoring fields in AutoMapper does just that, they are ignored and null when I try to match them before saving to my repository.

+6
asp.net-mvc viewmodel mvvm fluent-nhibernate automapper
source share
4 answers

Asp.net mvc DefaultModelBinder is extensible and you can override it to create your own binding scheme. But this will require more work than the two "hidden input fields", which, in my opinion, are not so crude.

+2
source share

You can tell Automapper to ignore 2 properties:

Mapper.CreateMap<Source, Destination>() .ForMember(dest => dest.SomeValuefff, opt => opt.Ignore()); 

Possible related question .

+2
source share

I think you will find a solution to your problem here http://www.codethinked.com/aspnet-mvc-think-before-you-bind (a couple of possible solutions)

I have the same problem and I think hidden filds arent is the solution!

0
source share

Can you use AutoMapper.Map overload, which also accepts TEntity ?!

 entity = Mapper.Map(viewmodel, entity); 

As long as you have no properties in your view model, it will not change the value on your entity. It accepts the passed object and applies only the properties from the viewmodel to the object.

0
source share

All Articles