Hello,
I am having a development issue that I cannot seem to figure out here. So the layout is that I have a database that has multiple schemas and multiple tables that are joined giving a many to many. I have classes that I written
in C# for the MVC and when I go to make the views, I cannot seem to bind the other tables with my model. So here is what I have: A picture of the tables from SQL Server
Here is the Visual Studio representation from ADO EF6:
Here are the three classes for the view that I am trying to create:
Asset.cs
namespace HomeInventory.Web.Areas.Classes { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Spatial; [Table("Asset.Asset")] public partial class Asset { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Asset() { Locations = new HashSet<Location>(); Photos = new HashSet<Photo>(); } public int AssetID { get; set; } [Required] public string AssetName { get; set; } [Required] [StringLength(50)] public string AssetType { get; set; } [StringLength(255)] public string AssetSerial { get; set; } [Column(TypeName = "datetime2")] public DateTime AssetDate { get; set; } [Column(TypeName = "money")] public decimal? AssetPurCost { get; set; } [Column(TypeName = "money")] public decimal? AssetValue { get; set; } public string AssetNotes { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Location> Locations { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Photo> Photos { get; set; } } }
Location.cs
namespace HomeInventory.Web.Areas.Classes { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Spatial; [Table("Common.Location")] public partial class Location { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Location() { Assets = new HashSet<Asset>(); Books = new HashSet<Book>(); Musics = new HashSet<Music>(); Videos = new HashSet<Video>(); } public int LocationID { get; set; } [Required] public string LocationName { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Asset> Assets { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Book> Books { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Music> Musics { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Video> Videos { get; set; } } }
Photo.cs
namespace HomeInventory.Web.Areas.Classes { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Spatial; [Table("Common.Photo")] public partial class Photo { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Photo() { Assets = new HashSet<Asset>(); Books = new HashSet<Book>(); Musics = new HashSet<Music>(); Videos = new HashSet<Video>(); } public int PhotoID { get; set; } [Required] [StringLength(50)] public string PhotoName { get; set; } public byte[] PhotoStorage { get; set; } public Guid RowGuid { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Asset> Assets { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Book> Books { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Music> Musics { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Video> Videos { get; set; } } }
Here is the View:
@model HomeInventory.Web.Areas.Classes.Asset @{ ViewBag.Title = "Add Assets"; Layout = "~/Views/Shared/_AssetLayout.cshtml"; } @{ var lgrid = new WebGrid(source: ViewBag.Loca, defaultSort: "LocationName", rowsPerPage: 10, canSort: false, pageFieldName: "pg"); }<p id="pAAddHead" style="font-family:'Times New Roman', Times, serif; font-size:16px; text-align:center">Add Assets to Inventory</p> @using (Html.BeginForm()) { @Html.AntiForgeryToken()<div class="form-horizontal"><hr /><div class="form-group col-md-6"> @Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="form-group"> @Html.LabelFor(model => model.AssetName, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4"> @Html.EditorFor(model => model.AssetName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AssetName, "", new { @class = "text-danger" })</div></div><div class="form-group"> @Html.LabelFor(model => model.AssetDate, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4"> @Html.Editor("DatePicker", new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AssetDate, "", new { @class = "text-danger" })</div></div><div class="form-group"> @Html.LabelFor(model => model.AssetPurCost, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4"> @Html.EditorFor(model => model.AssetPurCost, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AssetPurCost, "", new { @class = "text-danger" })</div></div><div class="form-group"> @Html.LabelFor(model => model.AssetSerial, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4"> @Html.EditorFor(model => model.AssetSerial, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AssetSerial, "", new { @class = "text-danger" })</div></div><div class="form-group"> @Html.LabelFor(model => model.AssetType, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4"> @Html.EditorFor(model => model.AssetType, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AssetType, "", new { @class = "text-danger" })</div></div><div class="form-group"> @Html.LabelFor(model => model.AssetNotes, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-4"> @Html.EditorFor(model => model.AssetNotes, new { htmlAttributes = new { @class = "form-control col=21 rows=5" } }) @Html.ValidationMessageFor(model => model.AssetNotes, "", new { @class = "text-danger" })</div></div></div><div class="form-group col-md-6"> @Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="form-group"> @Html.LabelFor(model => model.AssetValue, htmlAttributes: new { @class = "control-label col-md-3" })<div class="col-md-3"> @Html.EditorFor(model => model.AssetValue, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AssetValue, "", new { @class = "text-danger" })</div></div><div class="form-group"> @Html.LabelFor(model => model.Locations, htmlAttributes: new { @class = "control-label col-md-3" })<div class="col-md-3"> @lgrid.GetHtml(tableStyle: "grid", columns: lgrid.Columns(lgrid.Column(format: (Locations) => Html.CheckBox("LocationID")), lgrid.Column("LocationName", ""))) @Html.ValidationMessageFor(model => model.Locations, "", new { @class = "text-danger" })</div></div><div class="form-group"> @Html.LabelFor(model => model.Photos, htmlAttributes: new { @class = "control-label col-md-3" })<div class="col-md-3"> @Html.Editor("File", new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Photos, "", new { @class = "text-danger" })</div></div></div><div class="form-group"><div class="col-md-offset-2 col-md-4"><input type="submit" value="Add" class="btn btn-default" /><input type="button" value="Cancel" class="btn btn-danger" /></div></div></div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
and here is the portion of the controller for that view:
public ActionResult AddAsset([Bind(Include = "AssetID,AssetName,AssetType,AssetSerial,AssetDate,AssetPurCost,AssetValue,AssetNotes")] Assets asset) { if (ModelState.IsValid) { adb.Assets.Add(asset); await adb.SaveChangesAsync(); return RedirectToAction("AssetMain"); } { var _loc = new List<Location>(); _loc = cdb.Locations .OrderBy(l => l.LocationName) .ToList(); ViewBag.Loca = _loc; return View(); }
Now first off When I generated the view it gave me the information for the Asset table everything in the view up to locations. I had to add Locations and Photos, I cannot get any of the locations in the database to show up, though they show up in the view for creating, editing, deleting the locations, and in another view I am trying to work using the exact same code. Plus I cannot get a file browser to show up for the photos... I have tried a few different pieces of code there, but nothing works. I have also tried submit data to the database, because it doesn't necessarily need a photo at this time, and nothing was sent to the database. So I am wondering where I am going wrong with this. I guess what I thought was slightly simple has put me in way over my head. Any help would be appreciated.
Michael R. Mastro II