1

ViewModel:

namespace CFMembers.ViewModels
{
    public class IndexData
    {

        public LicenseHolder LicenseHolders { get; set; }
        public Installation Installations { get; set; }

    }
}

Controller:

public ActionResult Create()
{

        LicenseHolder licenseHolder = new LicenseHolder();



        IndexData ViewData = new IndexData
        {
            LicenseHolders = licenseHolder,

        };

        PopulatePartyTypeDropDownList();
        PopulateLicenseHolderTypeDropDownList();
        return View(ViewData);

    }

    //
    // POST: /Main/Create

    [ActionName("Create"), AcceptVerbs(HttpVerbs.Post)]
    public ActionResult CreateSubmit(IndexData viewModel)
    {


        if (ModelState.IsValid)
        {

            db.LicenseHolder.Add(viewModel.LicenseHolders);
            db.Installation.Add(viewModel.Installations);

            db.SaveChanges();
            return RedirectToAction("Index");
        }


        return View();
    }

Create.cshtml

<div class="editor-label">
    @Html.LabelFor(model => model.LicenseHolders.LicenseHolderTypeID, "License Type")
 </div>
 <div class="editor-field">
    @Html.DropDownList("LicenseHolderTypeID", String.Empty)
    @Html.ValidationMessageFor(model => model.LicenseHolders.LicenseHolderTypeID)               
</div>

@Html.DropDownList work just fine on another view. However that was not on a viewmodel with more than one table. When it's posted back LicenseHolderTypeID is 0 not matter what I do. I've also tried dropdownlistfor and still get 0. Any assistants would be greatly appreciated. I think it's to do with how the viewmodel is passed back but i'm stuck.

4

0 回答 0