Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC Multiple tables one model
    primarykey
    data
    text
    <p>I am having difficulty with my understanding of MVC coming from an aspx world.</p> <p>I have a Model called <code>CustomerGarment</code>. This has a <code>Order</code> and a <code>Customer</code> along with a few garments.</p> <pre><code>public class CustomerGarment { public int CustomerGarmentId { get; set; } public virtual Customer Customer { get; set; } public virtual Order Order { get; set; } public virtual GarmentJacket GarmentJacket { get; set; } public virtual GarmentShirt GarmentShirt { get; set; } } </code></pre> <p>I have a method for get and post. When the page loads, it creates a new <code>CustomerGarment</code> instance and querys the database to fill the Customer and Order variables. I then use the viewbag to show on the screen a list of <code>GarmentJacket</code>s and <code>GarmentShirt</code>s</p> <p>The page then views and using the view I can access the model perfectly. Drop downs load with the viewbag contents and I can access all Customer and Order variables using the model I have passed.</p> <p>The problem I then face is when I use the HttpPost. The model is not passed back with the information I passed to it. </p> <pre><code> public ActionResult AddGarments(int orderId, int customerId) { CustomerGarment cg = new CustomerGarment(); cg.Order = (from a in db.Orders where a.OrderId == orderId select a).FirstOrDefault(); cg.Customer = (from a in db.Customers where a.CustomerId == customerId select a).FirstOrDefault(); var jackets = from a in db.GarmentJackets orderby a.Type, a.SleeveLengthInches, a.ChestSizeInches select a; var shirts= from a in db.GarmentKilts orderby a.PrimarySize, a.DropLength select a; ViewBag.GarmentJacket = new SelectList(jackets, "GarmentJacketId", "GarmentJacketId"); ViewBag.GarmentShirt = new SelectList(shirts, "GarmentShirtId", "GarmentShirtId"); return View(cg); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult AddGarments(CustomerGarment cg) { // Here, I do not have the customer info for example db.CustomerGarments.Add(cg); db.SaveChanges(); return RedirectToAction("Index"); return View(cg); } </code></pre> <p>This is a bit of my view</p> <pre><code>@Html.HiddenFor(model =&gt; model.Order.OrderId) @Html.HiddenFor(model =&gt; model.Order.CustomerId) &lt;div class="display-field"&gt; @Html.DisplayFor(model =&gt; model.Customer.Name) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.GarmentJacket, "Jacket") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(m =&gt; m.GarmentJacket, (SelectList)ViewBag.GarmentJacket, new {style="width:312px;height:30px;margin-top:2px;margin-bottom:5px"}) &lt;/div&gt; </code></pre> <p><strong>EDIT</strong></p> <p>My Garment Jacket Model</p> <pre><code>public class GarmentJacket : Garment { public int GarmentJacketId { get; set; } [Required] public string Type { get; set; } [Required] [Display(Name = "Chest Size")] public int ChestSizeInches { get; set; } [Required] [Display(Name = "Sleeve Length")] public int SleeveLengthInches { get; set; } } public class Garment { [DataType(DataType.Date)] public DateTime? DateRetired { get; set; } [Required] public string Barcode { get; set; } [Required] public bool Adults { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload