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

WorknHardrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
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

0
WorknHardrAuthor Commented:
hmm, what is 'ModelTypeOptions'???
0
Bob LearnedCommented:
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");
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

WorknHardrAuthor Commented:
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
0
Bob LearnedCommented:
Add it to the view model:

public class VendorViewModel
 {
        public Vendor vendor { get; set; }
        public VendorType vendorType { get; set; }
        public SelectList VendorTypeSelectList { get; set; }
 }

Open in new window

Pass into view model from the context:

VendorViewModel viewModel = new VendorViewModel
{
     Vendor = new Vendor(),
     VendorType = new VendorType()
     VendorTypeSelectList = new SelectList(context.VendorTypes, "VendorTypeID", "Type");
 };

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WorknHardrAuthor Commented:
Excellent, I lean something new everyday, thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.