Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


olu adesina
olu adesina
23,007 Points

Validation Exception on select list of entities that have already been validated

I'm using a ViewModel to combine two entities in one view. for the first entity im using textboxfor & lablefor html helpers to gather data for a post request. For the second entity Im using a dropdownlistfor which is a selectlist of a group of address objects which are entered in another view. i have used data annotations on my address class to make all fields required. once entries have passed validation and saved to my database via the add address view. when i try to select it with a view model i get validation exception indicating that the fields are required on my address entries

if i remove validation on the address class it seems to work

my controller

public ActionResult AddMember()
        var ViewMember = new MemberViewModel(); 
            ViewMember.AddressList = new SelectList(_context.Addresses, "Id", "streetName");

        return View(ViewMember);

    public ActionResult AddMember(MemberViewModel Entry)
        var entry = new Member();
        entry = Entry.member;

        entry.Address = new Address() {Id= Entry.addressId };

        if (ModelState.IsValid)

            repository.AddMember(entry, _context);

            TempData["message"] = "Your Member was added";

            return RedirectToAction("Members", new { id = entry.Id });
        return View();

my view

@model  AdesinaWebApp.ViewModels.MemberViewModel
  ViewBag.Title = "AddMember";
  Layout = "~/Views/Shared/_Layout.cshtml";


@using (Html.BeginForm()){

@Html.LabelFor(fn => fn.member.Name)<br>
@Html.TextBoxFor(fn => fn.member.Name)<br /><br />

@Html.LabelFor(fn => fn.member.LastName)<br>
@Html.TextBoxFor(fn => fn.member.LastName)<br />

@*@Html.LabelFor(fn => fn.address.DoorNumber)<br>
@Html.TextBoxFor(fn => fn.address.DoorNumber)<br /><br />

@Html.LabelFor(fn => fn.address.streetName)<br>
@Html.TextBoxFor(fn => fn.address.streetName)<br /><br />

@Html.LabelFor(fn => fn.address.PostCode)<br>
@Html.TextBoxFor(fn => fn.address.PostCode)<br /><br />*@

@Html.DropDownListFor(m=>m.addressId,Model.AddressList,"please select your address")

<button type="submit">Submit</button>


my view model

 public class MemberViewModel


    public int addressId { get; set; }

    public Address address { get; set; }

    public Member member { get; set; } = new Member();

   public SelectList AddressList { get; set; }


my address class

public class Address
    public int Id { get; set; }
    [Required(ErrorMessage = "Please enter a Door Number")]
    public int DoorNumber { get; set; }
    [Required(ErrorMessage = "Please enter a Street Name.")]
    public string streetName { get; set; }
    [Required(ErrorMessage = "Please enter a Post Code.")]
    public string PostCode { get; set; }

    public ICollection<Member> Members { get; set; }

your help will be much appreciated