views:

209

answers:

1

I have searched like a fool but does not get much smarter for it..

In my project I use Entity Framework 4 and own PoCo classes and I want to use DataAnnotations for validation. No problem there, is how much any time on the Internet about how I do it. However, I feel that it´s best to have my validation in ViewModels instead and not let my views use my POCO classes to display data. How should I do this smoothly? Since my repositories returns obejekt from my POCO classes I tried to use AutoMapper to get everything to work but when I try to update or change anything in the ModelState.IsValid is false all the time..

My English is really bad, try to show how I am doing today instead: My POCO

public partial User {  
    public int Id { get; set; }  
    public string UserName { get; set; }  
    public string Password { get; set; }  
}

And my ViewModel

public class UserViewModel {
    public int Id { get; set; }

    [Required(ErrorMessage = "Required")]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Required")]
    public string Password { get; set; }
}

Controller:

public ActionResult Edit(int id) {
    User user = _userRepository.GetUser(id);
    UserViewModel mappedUser = Mapper.Map<User, UserViewModel>(user);

    AstronomiGuidenModelItem<UserViewModel> result = new AstronomiGuidenModelItem<UserViewModel> {
        Item = mappedUser
    };

    return View(result);
}

[HttpPost]
public ActionResult Edit(UserViewModel viewModel) {
    User user = _userRepository.GetUser(viewModel.Id);
    Mapper.Map<UserViewModel, User>(viewModel, user);       

    if (ModelState.IsValid) {
        _userRepository.EditUser(user);

        return Redirect("/");
    }

    AstronomiGuidenModelItem<UserViewModel> result = new AstronomiGuidenModelItem<UserViewModel> {
        Item = viewModel
    };

    return View(result);
}

I've noticed now that my validation is working fine but my values are null when I try send and update the database. I have one main ViewModel that looks like this:

public class AstronomiGuidenModelItem<T> : AstronomiGuidenModel {
    public T Item { get; set; }
}

Why r my "UserViewModel viewModel" null then i try to edit?

+1  A: 

If the validation is working, then UserViewModel viewModel shouldn't be null... or is it that the client side validation is working but server side isn't?

If that's the case it could be because of the HTML generated.

For instance, if in your view you have:

<%: Html.TextBoxFor(x => x.Item.UserName) %>

The html that gets rendered could possibly be:

<input name="Item.UserName" id="Item_UserName" />

When this gets to binding on the server, it'll need your action parameter to be named the same as the input's prefix (Item). E.g.

public ActionResult Edit(UserViewModel item) {

To get around this, do as above and change your action parameter to item OR you could encapsulate the form into a separate PartialView which takes the UserViewModel as it's model - that way the Html.TextBoxFor won't be rendered with a prefix.

HTHs,
Charles

Ps. If I'm totally off track, could you please post some code for the view.

Charlino
I found the silly problem, `UserViewModel` sould be `AstronomiGuidenModelItem<UserViewModel>`.
Timmie Sarjanen