1

I've been struggling with this all day this is my first ever post so apologizes if its difficult to understand or not formatted well

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);
    }




    [HttpPost]
    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";
}

 <h2>AddMember</h2>

@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.LabelFor(m=>m.addressId)
@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
{
    [Key]
    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; }
}


     public class Member
   {
    [Key]
    [Required]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string LastName { get; set; }


    public Address Address { get; set; }
    }

your help will be much appreciated

Rocky
  • 11
  • 5
  • You are using `entry.Address = new Address() { ... }` to add a new `Address`, but the only property that you set is the `Id` property (you did not set any values for `DoorNumber`, `streetName ` etc which are required. –  Aug 09 '18 at 02:09
  • Having said that, its not clear what your dropdownlist is supposed to be doing, and why you are using that to set the Primary Key property of `Address` (which cannot be done and makes no sense) –  Aug 09 '18 at 02:11
  • my dropdown list is meant to give users a set choices to choose from which have been already saved in the database instead of entering dupilcates – Rocky Aug 09 '18 at 02:19
  • Then you code is making even less sense :) –  Aug 09 '18 at 02:21
  • what would u suggest – Rocky Aug 09 '18 at 02:22
  • You need to show us the `Member` model –  Aug 09 '18 at 02:23
  • public class Member { [Key] [Required] public int Id { get; set; } [Required] public string Name { get; set; } [Required] public string LastName { get; set; } public Address Address { get; set; } } – Rocky Aug 09 '18 at 02:25
  • In the question, not in comments –  Aug 09 '18 at 02:26
  • my bad. im new to this – Rocky Aug 09 '18 at 02:28
  • Your `Member` model does not contain a navigation property to the `Address` table –  Aug 09 '18 at 02:32
  • public Address Address { get; set; } this is my navigation property – Rocky Aug 09 '18 at 02:36
  • No its not (suggest you read through [Entity Framework Relationships and Navigation Properties](https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).aspx) to understand what you models need to be –  Aug 09 '18 at 02:38
  • ok will have a look at Entity Framework Relationships and Navigation Properties – Rocky Aug 09 '18 at 02:40
  • But there are numerous other issues with you code.First, view models do not contain data models - they contain the properties of your data models that you need in the view - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). Next, how many of your users are going to have the same address? If you have 1000 users, you will most likely have 1000 separate adresses, which is far too much to display in a dropdownlist. –  Aug 09 '18 at 02:41
  • I know what u mean about the drop down list I was attempting to add this feature just for practice – Rocky Aug 09 '18 at 02:45
  • I will read up on both the links provided – Rocky Aug 09 '18 at 02:46
  • Well that is not the right way to go about it. (and you may as well include the properties for `Address` in your `Member` table since its really just a one-one relationship). What you would do is have (say) autocomplete controls for the `Suburb` (and `Country` if applicable) properties, so that typing one or 2 characters would display a list of suburbs or countries starting with those characters (making it a better UI experience) –  Aug 09 '18 at 02:50

0 Answers0