WorknHardr
asked on
MVC ViewModel 2nd Model Posts Null?
I have a 'VendorViewModel' which displays data to the View without error. The 1st model (Vendor) returns data fine. The 2nd model (VendorType) is always null.
[ViewModel]
[View]
[Action's]
[ViewModel]
public class VendorViewModel
{
public Vendor vendor { get; set; }
public VendorType vendorType { get; set; }
}
public partial class Vendor
{
public Vendor()
{
this.Expenses = new HashSet<Expense>();
}
public System.Guid VendorID { get; set; }
public string Name { get; set; }
public System.Guid VendorTypeID { get; set; }
public string Description { get; set; }
public virtual ICollection<Expense> Expenses { get; set; }
public virtual VendorType VendorType { get; set; }
}
public partial class VendorType
{
public VendorType()
{
this.Vendors = new HashSet<Vendor>();
}
public System.Guid VendorTypeID { get; set; }
public string Type { get; set; }
public virtual ICollection<Vendor> Vendors { get; set; }
}
[View]
@using (Html.BeginForm("New", "Vendor", FormMethod.Post, new { id = "form1" }))
{
<fieldset>
<legend>Add New Vendor To The List</legend>
<div class="editor-label">
@Html.LabelFor(model => model.vendor.Name, "Name")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.vendor.Name, "Name")
</div>
<div class="editor-label">
@Html.Label("Type")
</div>
<div class="editor-field">
@Html.DropDownList("VendorTypeID", ViewBag.VendorTypeID as SelectList, "- select -")
</div>
<div class="editor-label">
@Html.LabelFor(model => model.vendor.Description, "Description")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.vendor.Description, "Description")
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
[Action's]
public ActionResult _New()
{
//Doesn't make any difference
VendorViewModel viewModel = new VendorViewModel
{
vendor = new Vendor(),
vendorType = new VendorType()
};
ViewBag.VendorTypeID = new SelectList(context.VendorTypes, "VendorTypeID", "Type");
return View(viewModel);
}
public ActionResult New(VendorViewModel vvm)
{
if (ModelState.IsValid)
{
context.VendorTypes.Add(vvm.vendorType); // NULL
context.Vendors.Add(vvm.vendor);
context.SaveChanges();
return RedirectToAction("_New");
}
return View("_New");
}
ASKER
hmm, what is 'ModelTypeOptions'???
Sorry, that was an example without context. SelectList constructor takes any list that implements IEnumerable.
I believe that would be equivalent to this:
new SelectList(context.VendorT ypes, "VendorTypeID", "Type");
I believe that would be equivalent to this:
new SelectList(context.VendorT
ASKER
That's what I thought, just unsure how to get 'context' within the View. I do this in the controller like so:
BudgetProjectEntities context = new BudgetProjectEntities();
context.VendorTypes
BudgetProjectEntities context = new BudgetProjectEntities();
context.VendorTypes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent, I lean something new everyday, thx
Open in new window
Try using the HtmlHelper.DropDownListFor
Open in new window