0

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?

2
  • Why are you POSTing an empty title? Commented Dec 14, 2011 at 14:36
  • @SLaks, because i don't want user to edit Title - only to display it. so it is not placed in TextEditor. Commented Dec 14, 2011 at 14:39

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

12 Comments

+1 I don't know automapper but this sounds like a better solution then mine. cheers
yes, i create this mapping but without instructions. thank you, i'll try it(=
Could he also, consider removing the setter from ZoneView.Title as to only accept it through the ZoneView constructor? This way the default model binder wont be able to set the property but he could still display it and that logic becomes a part of the view. Of course the view could not be used elsewhere that allows editing of the Title like in a admin panel though. Just curious what you think?
He certainly could, but I think that's just jumping through too many hoops unnecessarily. In practice, view models should be very dumb and straight forward, concerned only with specifying the information you want to display or collect, where all the complicated facets of the domain model are flattened or otherwise simplified.
As I mentioned my thought was he could take it through the constructor but fair enough. To your point I have been using the concept of viewmodels for years now and never put logic business logic in them, was just curious and unfamiliar with using automapper. thanks
|
0
  • Most of the time i use Hidden input such values.
  • And also check at DB level for validation of value with title.

This might not best solution but it works.

1 Comment

hidden input can be changed by user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.