2

I have a ViewModel that, among other properties, contains an array of 'EmailAddress' objects. EmailAddress itself has properties such as "Address", and "IsPrimary". My view model breakdown is:

public class UserDetailsViewModel {
  public BUser User { get; set; }
  public string[] Roles { get; set; }
  public EmailAddress[] EmailAddresses { get; set; }
}

I am showing a "User Details" page that is pretty standard. For example, I'm displaying data using @Html.DisplayFor(model => model.User.UserName) and @Html.DisplayFor(model => model.User.Comment) I also have a section on the page that lists all of the EmailAddress objects associated with the user:

@if(Model.EmailAddresses.Length > 0) {
  foreach (var address in Model.EmailAddresses) {
    <div>
      @Html.DisplayFor(model => address.Address)
    </div>
  }
} else {
  <div class="center">User does not have any email addresses.</div>
}

My problem is that I would like to show an "Add Email Address" form above the list of email addresses. I thought I would take the "normal" approach thusly:

@using(Html.BeginForm(new { id=Model.User.UserName, action="AddUserEmailAddress" })) {    
  <text>Address:</text> @Html.EditorFor(model => ** HERE I AM STUCK **)
  <input type="submit" value="Add Email" class="button" />
}

As you may be able to tell, I am stuck here. I've tried model => Model.EmailAddresses[0] and model => Model.EmailAddresses.FirstOrDefault(). Both of these have failed horribly.

I am sure that I am going about this all wrong. I've even thought of adding a "dummy" property to my ViewModel of type EmailAddress just so that I can bind to that in my EditorFor - but that seems to be a really bad hack. There has to be something simple that I'm overlooking!

I would appreciate any help anyone can offer with this matter. Thank you in advance!

2 Answers 2

1

You could go the quick&dirty way of using :

@Html.TextBox("NewEmail")

In your AddUserEmailAddress controller method you will be able to get the posted value by using :

Request.Form.GetValues("NewEmail")

or

Request["NewEmail"]

If you don't want to loose the built in validation, as you stated, you can always create another property in your ViewModel ? Supply your view with a form, or load it dynamically with jQuery.load("/Controller/AddEmail/UserID"). Add

public string NewEmail { get; set; } 

in your ViewModel, and the below in your view

@Html.EditorFor(model=>model.NewEmail)

Plus, you may consider using strongly typed structure for your emails like List that gives you more control over your data in particular situations.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought of that, but then I don't get the automatic model validation (both client and server side).
1

I'm pretty new to MVC, but I've already tackled this problem, I use @Html.Partial for that.

@Html.Partial("path/to/EditorTemplate", new EmailAddress())

I also tried to take the EmailAddresses[0] route, I remember that day fondly. I'm also not positive it is the best way, but it plays nicely with my other code.

Comments

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.