1

Very new to ASP.net Core 2 MVC but trying to figure things out.

I have created a ViewModel:

ublic class PeopleStateViewModel
{
    public People People { get; set; }
    public StatesDictionary States { get; set; }

    public PeopleStateViewModel(People people)
    {
        People = people;
        States = new StatesDictionary();
    }
}

I Have these two models:

    public class People
{
    public int ID { get; set; }
    [StringLength(60, MinimumLength = 2)]
    [Required]
    public string FirstName { get; set; }
    [StringLength(60, MinimumLength = 2)]
    [Required]
    public string LastName { get; set; }
}



    public static SelectList StateSelectList
    {
        get { return new SelectList(StateDictionary, "Value", "Key"); }
    }
    public static readonly IDictionary<string, string>
        StateDictionary = new Dictionary<string, string> {
            {"Choose...",""}
            , { "Alabama", "AL" }
            , { "Alaska", "AK" }
            , { "Arizona", "AZ" }
            , { "Arkansas", "AR" }
            , { "California", "CA" }
            // code continues to add states...
        };
      }

I try to create a controller using MVC Controller with views, using Entity Framework.

enter image description here

I then get this error:

enter image description here

I want to be able to use data from both models data on a view...

Any help is appreciated.

Tim
  • 563
  • 2
  • 6
  • 20
  • 1
    `PeopleStateViewModel` viewmodel certainly doesn't have any primary key field, hence it cannot be scaffolded. Use your actual data model instead of a viewmodel to perform scaffolding. – Tetsuya Yamamoto Sep 08 '17 at 03:23
  • 1
    A view model has no relationship with your data context. And as a side note, they do not contain properties which are data models - just the properties of your data models that your need in the view. And your view model would throw an exception in the POST method because it does not have a parameterless constructor –  Sep 08 '17 at 03:29

2 Answers2

2

You can't create a scaffolding by using PeopleStateViewModel since it doesn't have primary key defined (you need to use actual data model from database). You can add the ID property with KeyAttribute in People data model & perform scaffolding on this data model instead:

public class People
{
    [Key]
    public int ID { get; set; }

    [StringLength(60, MinimumLength = 2)]
    [Required]
    public string FirstName { get; set; }

    [StringLength(60, MinimumLength = 2)]
    [Required]
    public string LastName { get; set; }
}

Additionally using non-parameterless constructor in the viewmodel on form submit with HttpPost like this:

[HttpPost]
public ActionResult ActionName(PeopleStateViewModel model)
{
    // do something
}

It will throw this exception:

System.MissingMethodException: No parameterless constructor defined for this object.

To prevent that error, declare a parameterless constructor on the viewmodel class:

public class PeopleStateViewModel
{
    public People People { get; set; }
    public StatesDictionary States { get; set; }

    public PeopleStateViewModel()
    {
    }

    public PeopleStateViewModel(People people)
    {
        People = people;
        States = new StatesDictionary();
    }
}
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
1

As mentioned in the comments, domain models should be used in scaffolding controllers and views. In your case use the People model when scaffolding. After the controller and views are created, start modifying the controller and views to use the PeopleStateViewModel.

corix010
  • 551
  • 8
  • 25