tags:

views:

100

answers:

1

I know the header is a bit hard to figure out, but I didn't exactly know how to describe the problem, but here it comes.

I have this domain entity: User and it has a list of the subclass Address, since the user can have multiple address. Home, Work etc etc...

private class User{
   private string _FirstName;
   private string _LastName;
   private IList<Address> _addresses;

   public string FirstName{...}
   public string LastName{...}
   public IList<Addresses> Addresses {...}
}  

private class Address{
  private string _streetAddress1;
  private string _streetAddress2;
  ...

  public string StreetAddress1{...};
  public string StreetAddress2{...};
  ...
}

I have a Validation of the inputdata that looks like this.

[AcceptVerbs(HttpVerbs.Post)]
[ValidateModelAttribute(typeof(FormUser))]
public ActionResult CreateEdit([Bind(Prefix = "")] FormUser formUser)
{
    //-- Check so the input
    if (!ModelState.IsValid)
       return View();

    User modelUser = new UserMapper().MapToModel(formUser);
    _UserRepository.Save(modelUser);

    return RedirectToAction<SharedController>(m => m.Success());
}

The FormUser is my PresentationEntity that looks like this.

public class FormUser
{
    [NonEmptyValidator("Field is required!")]   
    public string FirstName{get;set;}
    [NonEmptyValidator("Field is required!")]    
    public string LastName{get;set;}

    public List<Address> Addresses{get;set;}
}

For the FirstName and LastName filed this is no problem it's very simple and works greate. But now I have added the Addresses property to my User entity and want to make this input in a smart way with easy validation of the input field in the Address entity.

So say I have the FirstName and LastName textboxes in my webpage, and now I want to add an Address for this User. I can see a way to do this

It is to open an new webpage that has it's own controller that makes the same validation that I do for the User. So I have a FormAddress class and CreateEdit()- method in my AddressController. The problem I see here is how do I add this address-data to the user when I am finished validating the data?

Or is there a better way to do this. It's really easy in a stateful applicaiton, but here I can't see a good clean way to do it.

A: 

Your form on the view can contain fields for the User and the Address. Have this form go to a single ActionResult that requires a User and an Address. The framework will map the fields from your form to the properties in your domain model classes (as long as you name the form fields with the same names as the class properties).

public ActionResult CreateEdit(User user, Address address)

{
//validate the user and the address here (how is up to you)

//add the user

//add the address to the user

//save to the db
}

Now, in your ActionResult you can perform validation on the User object and the Address object separately.

mikerennick
The problem are that the User can have multiple Addresses, so I want to have a seperate page for entering the address data. Thats because there are much more information on the User than just the address, there are diffrent phonenumbers, there will be reference to other user objects and lot more, so I need to keep the "User page" as simple as possible, and have the other input forms in separete pages or layers that can be shown/hidden when we need them. And since there has too be validation on all parts I can't validate everything everytime since all data will not be supplied at all time.
Magnus Gladh
How are you show/hiding your forms? One way is via tabs with each area its own view. This approach makes life easy, but causes round trips to the server for each tab loaded. You can also use JQuery to pop open a window with an Address form (or phone, or whatever). Each form would need to have its own form tags with a form action that points to a distinct controller action. Further, you could do the same with Jquery accordian, or tabs. Also, don't nest your forms -- nested forms are not allowed.
mikerennick