Link to home
Start Free TrialLog in
Avatar of WorknHardr
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]
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; }
    }

Open in new window


[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>
            }

Open in new window


[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");
        }

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

You are doing something custom, so the property names won't line up:

 
<div class="editor-field">
                        @Html.DropDownList("VendorTypeID", ViewBag.VendorTypeID as SelectList, "- select -")
</div>

Open in new window


Try using the HtmlHelper.DropDownListFor:

Html.DropDownListFor(m => m.ModelType, 
                new SelectList(Model.ModelTypeOptions, 
                               "ModelId", "Value", 
                               Model.ModelTypeOptions.First().ModelId))

Open in new window

Avatar of WorknHardr
WorknHardr

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.VendorTypes, "VendorTypeID", "Type");
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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent, I lean something new everyday, thx