Reputation: 842
recently i've started to use viewmodels with automapper. the problem appeared immediately. for example, i have such classes:
public class Zone
{
public int ZoneId;
public string Title;
public int BannersCount;
}
public class ZoneView
{
public int ZoneId
{
get;
set;
}
public string Title;
{
get;
set;
}
[Required(ErrorMessage = "Поле Кол-во баннеров является обязательным.")]
[Display(Name = "Кол-во баннеров*")]
public int BannersCount
{
get;
set;
}
}
and i don't want to edit Title in my view. so there i show Title not in TextEditor but so:
@Model.Title
and then in my POST action ZoneView comes with empty Title:
public ActionResult Edit(ZoneView zoneView)
after this i map it to domain model:
var zone = zonesRepository.Get(zoneView.ZoneId);
Mapper.Map<ZoneView, Zone>(zoneView, zone);
and after this in zone there is empty Title. What is the best way to act in this case?
Upvotes: 0
Views: 583
Reputation: 17485
This might not best solution but it works.
Upvotes: 0
Reputation: 42497
You can create a mapping definition, instructing automapper to ignore the Title property when pushing values into Zone.
Mapper.CreateMap<ZoneView, Zone>()
.ForMember(destination => destination.Title, member => member.Ignore());
You will define this CreateMap once, at application start up. Then map as you have been.
var zone = zonesRepository.Get(zoneView.ZoneId);
Mapper.Map<ZoneView, Zone>(zoneView, zone);
Upvotes: 2