0

i'm trying to get the Id of a DropdownList into a model using Enum but I always get null inside the variable of the Model. Hier ist my Code.

Model:

public partial class Vehicule
{
    public int vehiculeID { get; set; }
    public Nullable<int> paidTextID { get; set; }
}

the Enum Class

public class EnumClass
{
    public enum Paid
    {
        Yes = 1 ,
        No = 2  ,
        NotComplete= 3,
    }

}

and the View

<div class="form-group">
        @Html.LabelFor(model => model.paid, new { @class = "control-label col-md-2" })*
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.paidTextID, Enum.GetValues(typeof(EnumClass.Paid)).Cast<EnumClass.Paid>().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }), new { style = "width: 500px" })
            @Html.ValidationMessageFor(model => model.paid)
        </div>
    </div>

The List is populated but when I select I till have null inside the paidTextID.

Please Help

Thx

evalls
  • 39
  • 5
  • What are you trying to do here? Your over complicating this. Just make your `paidTextID` typeof `Paid` and use the `@Html.EnumDropDownListFor()` method –  Feb 08 '18 at 22:59
  • And your `LabelFor()` and `ValidationMessageFor()` make no sense - they are for a different property –  Feb 08 '18 at 23:03
  • If you dont have MVC-5, then refer [How do you create a dropdownlist from an enum in ASP.NET MVC?](https://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) –  Feb 08 '18 at 23:05
  • Ok Thanks I will try to do that this evening. But my Question ist. If I change the Type of paidTextID to paid, to wich data type does it correspond in entity framework. Because I have to save letter in my data base. For example String correspond to varchar what is about the enum Paid? Thx. – evalls Feb 09 '18 at 12:19
  • I tried to change paidTextID to Emum Paid like this: public EnumClass.Paid paidTextID { get; set; } but i'm getting an error while while getting data of the vehicule Model at this line : Zeile 36: var vehicules = await db.Vehicule.ToListAsync(); the error ist : "The entity type is not part of the model for the current context" – evalls Feb 09 '18 at 16:26
  • Hi Stephen, I still need some help. I created an Enum inside my model like this: `public Nullable paidTextID { get; set; }` and change the view to this `DropDownListFor(model => model.paidTextID, Enum.GetValues(typeof(Paid)).Cast().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }), new { style = "width: 500px" })` but still the paidTextId is null when I chose an Item. Please help – evalls Feb 10 '18 at 08:03

0 Answers0