MVC DropDownListFor not populating the selected value

Posted by user2254436 on Stack Overflow See other posts from Stack Overflow or by user2254436
Published on 2014-06-03T09:21:07Z Indexed on 2014/06/03 9:24 UTC
Read the original article Hit count: 195

I'm really having troubles with MVC, in another project I've done the same thing and it worked fine but in this project I just don't understand why the selected item in the dropdown is not populating the class correctly with EF.

I have 2 classes:

public partial class License
{
    public License()
    {
        this.Customers = new HashSet<Customer>();
    }

    public int LicenseID { get; set; }
    public int Lic_LicenseTypeID { get; set; }
    public int Lic_LicenseStatusID { get; set; }
    public string Lic_LicenseComments { get; set; }

    public virtual EntitiesList LicenseStatus { get; set; }
    public virtual EntitiesList LicenseType { get; set; }
}

public partial class EntitiesList
{
    public EntitiesList()
    {
        this.LicensesStatus = new HashSet<License>();
        this.LicensesType = new HashSet<License>();
    }

    public int ListID { get; set; }
    public string List_EntityValue { get; set; }
    public string List_Comments { get; set; }
    public string List_EntityName { get; set; }

    public virtual ICollection<License> LicensesStatus { get; set; }
    public virtual ICollection<License> LicensesType { get; set; }

    public string List_DisplayName
    {
        get
        {
            return Regex.Replace(List_EntityName, "([a-z])([A-Z])", "$1 $2"); ;
        }
    }

    public string List_DisplayValue
    {
        get
        {
            return Regex.Replace(List_EntityValue, "([a-z])([A-Z])", "$1 $2");
        }
    }
}

The EntitiesList is table in db that have all my "enum" lists. For example:

ListID - 0
List_EntityValue - Activate
List_EntityName - LicenseStatus

ListID - 1
List_EntityValue - Basic
List_EntityName - LicenseType

This is my model:

public class LicenseModel
{
    public License License { get; set; }
    public SelectList LicenseStatuses { get; set; }
    public int SelectedStatus { get; set; }
    public SelectList LicenseTypes { get; set; }
    public int SelectedType { get; set; }
}

My controller for create:

public ActionResult Create()
{
    LicenseModel model = new LicenseModel();
    model.License = new License();
    model.LicenseStatuses = new SelectList(managerLists.GetAllLicenseStatuses(), "ListID", "List_DisplayValue");
    model.LicenseTypes = new SelectList(managerLists.GetAllLicenseTypes(), "ListID", "List_DisplayValue");
    return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(LicenseModel model)
{
    if (ModelState.IsValid)
    {
        model.License.Lic_LicenseTypeID = model.SelectedType;
        model.License.Lic_LicenseStatusID = model.SelectedStatus;
        managerLicense.AddNewObject(model.License);
        return RedirectToAction("Index");
    }

    return View(model);
}

managerLists and managerLicense are the managers that connect between the entities in db and the MVC UI, nothing special... they contains queries for adding new objects, getting the lists, editing and so on.

And the view for creating the License:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>License</legend>

        <div class="form-group">
            @Html.LabelFor(model => model.License.Lic_LicenseTypeID)
            @Html.DropDownListFor(model => model.SelectedType, Model.LicenseTypes, new { @class = "form-control" })
            <p class="help-block">@Html.ValidationMessageFor(model => model.License.Lic_LicenseTypeID)</p>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.License.Lic_LicenseStatusID)
            @Html.DropDownListFor(model => model.SelectedStatus, Model.LicenseStatuses, new { @class = "form-control" })
            <p class="help-block">@Html.ValidationMessageFor(model => model.License.Lic_LicenseStatusID)</p>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.License.Lic_LicenseComments)
            @Html.TextAreaFor(model => model.License.Lic_LicenseComments, new { @class = "form-control", rows = "3" })
            <p class="help-block">@Html.ValidationMessageFor(model => model.License.Lic_LicenseComments)</p>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

Now, when I'm trying to save the new license, when it gets to the db.SaveChanges() in the manager I'm getting:

"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

In breakpoint, the Lic_LicenseTypeID and Lic_LicenseStatusID are getting correctly the ID's from the selected item in the dropdown but the LicenseStatus and LicenseStatus properties are null.

What an I missing?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about model-view-controller