making viewmodel for one-to-many/many-to-many relationship tables

I’m trying to use a ViewModel as opposed to ViewBags to populate a dropdownlist.

Here is my Asset class

public partial class Asset { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Asset() { this.Licenses = new HashSet<License>(); } public int AssetID { get; set; } public Nullable<int> PersonID { get; set; } public virtual Person Person { get; set; } public virtual ICollection<License> Licenses { get; set; } } 

Here is my Person class

public partial class Person { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Person() { this.Assets = new HashSet<Asset>(); } public int PersonID { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Asset> Assets { get; set; } } 

Here is my License class

public partial class License { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public License() { this.Assets = new HashSet<Asset>(); } public int LicenseID { get; set; } public string LicenseName { get; set; } public Nullable<System.DateTime> Expiration { get; set; } public Nullable<int> SubscriptionID { get; set; } public Nullable<decimal> Cost { get; set; } public string LicenseCode { get; set; } public virtual Subscription Subscription { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Asset> Assets { get; set; } } 

So I’m trying to make a ViewModel to combine the three and came up with something like this

public class AssetViewModel { public int AssetID { get; set; } public int PersonID { get; set; } public IEnumerable<SelectListItem> Person { get; set; } public ICollection<License> Licenses { get; set; } } 

Here is my create view page (shortened) for only necessary information, I didn’t try making a license dropdownlist yet.

@model Project.ViewModel.AssetViewModel 

@{ ViewBag.Title = “Create”; }

<h2>Create New Asset</h2>

@using (Html.BeginForm()) { @Html.AntiForgeryToken()

<div class="form-horizontal"> @*<h4>Asset</h4>*@ <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @*@Html.HiddenFor(model => model.DataAdded)*@ <div class="form-group"> @Html.LabelFor(model => model.PersonID, "Person", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @*@Html.DropDownList("PersonID", null, htmlAttributes: new { @class = "form-control" })*@ @Html.DropDownListFor(m => m.PersonID , Model.Person, "", htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.PersonID, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Licenses, "License", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.ListBox("Licenses", (MultiSelectList)ViewBag.Licenses , htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Licenses, "", new { @class = "text-danger" }) </div> </div> 

}

Here is my create method in Asset controller

// GET: Asset/Create public ActionResult Create() { var model = new AssetViewModel(); return View(model); } 

but right now with the current code I have for making the Person dropdownlist, I get the following error “The ViewData item that has the key ‘PersonID’ is of type ‘System.Int32’ but must be of type ‘IEnumerable<SelectListItem>’.”

submitted by /u/ncredibles
[link] [comments]

Leave a Reply