0

I am using an enum to store gender data, and used snippet of code in the create controller action to populate a SelectListItem from the enum , then I used a partial view to populate a dropdownlist from the SelectListItem. The dropdownlist shows items correctly. But when I sumbit the form I am getting this exception error.

This is the Enum Definition

public enum GenderEnum { Male = 1, Female = 2 }
public class Enums
{
    public enum genderList { Male = 1, Female = 2 };
}
public class GenderModel
{
    public GenderModel()
    {
        GenderList = new List<SelectListItem>();
    }
    [Display(Name = "Gender")]
    public int GenderID { get; set; }
    public IEnumerable<SelectListItem> GenderList { get; set; }
}

This is my Controller Action

public ActionResult Create()
{
    ViewBag.nationalityId = new SelectList(db.Nationalities, "nationalityId", "nationalityDescription");
    GenderModel gm = new GenderModel();
    IEnumerable<GenderEnum> GenderTypes = Enum.GetValues(typeof(GenderEnum)).Cast<GenderEnum>();
    gm.GenderList = from gender in GenderTypes select new SelectListItem
    {
        Text = gender.ToString(),
        Value = ((int)gender).ToString()
    };
    ViewBag.GenderData = gm;
    return View();
}

I called the partial view from the create view like this

@{ var GenderModel = (recTest.Models.GenderModel)ViewBag.GenderData; }

@Html.Partial("~/Views/Shared/_GenderDropDown.cshtml", GenderModel)

and finally this is the partial view

@model recTest.Models.GenderModel

<div class="form-group">
    @Html.LabelFor(model => model.GenderID, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.GenderID, Model.GenderList, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.GenderID, "", new { @class = "text-danger" })
    </div>
</div>

when submit the form I agetting this error

Additional information: The model item passed into the dictionary is of type 'recTest.Applicant', but this dictionary requires a model item of type 'recTest.Models.GenderModel'.

mdawaina
  • 69
  • 8

0 Answers0