0

I'm struggling with dropdownlist tried several methods online and all failed will show the methods that I tried.

Objective Create a Reciept with date,reference... & country. Country is Required and should be a dropdownlist.

So the Table for Reciept("ID, Name, Date, Address, City, CountryList).

RecieptModel

public class TransactionModel
{
    public int ID { get; set; }
    public int Name{ get; set; }
    public DateTime Date { get; set; }
    public string Address { get; set;}
    public string City { get; set; }
    public CountryList CountryList { get; set; }
}

public class ApplicationDbContext : DbContext
{
    public DbSet<RecieptModel> Reciepts { get; set;}
    public DbSet<CountryList> coutryList { get; set; }
}

CountryList

public class CountryList
{
    public byte Id { get; set; }

    public enum Country
    {
        Germany,
        US,
        UK
    }
}

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,Date,City,CountryList")] Reciepts reciepts)
{
    if (ModelState.IsValid)
    {
        db.Reciepts.Add(reciepts);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(reciepts);
}

View

<div class="form-group">
    @Html.LabelFor(model => model.CountryList, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EnumDropDownListFor(model => model.CountryList)
        @Html.ValidationMessageFor(model => model.CountryList)
    </div>
</div>

This failed I looked for several examples I'm trying to do it without the use of javascript. In the End I just want to learn how to implement a Dropdownlist & save it to the database allot of the methods that I tried to implement failed in MVC5.

Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
authy
  • 5
  • 2
  • I'm not using any files I'm implementing I'm not using any external files or downloaded anything from nuget. What is the proper method to use a dropdownlist all the online materials use several methods. Some use IEnumerals but all fail – authy Oct 12 '18 at 21:00
  • Define what "fail" means. – Erik Funkenbusch Oct 12 '18 at 21:01
  • e.g. lets say I use public IENumerable CountryList where countrylist class contains ID, CountryName. If I use @Html.DropDownListFor(model => model.CountryList, new SelectList(Model.CountryList)). The error is Model.CountryList cannot convert from CountryList to IEnumerable – authy Oct 12 '18 at 21:04
  • IEnumerable is not an enum. An enum is a specific c# type – Erik Funkenbusch Oct 12 '18 at 21:14
  • In your code above, change `public CountryList CountryList { get; set; }` to `public CountryList SelectedCountry { get; set; }` and then change your razor code to use `model => model.SelectedCountry` (also change the Bind attribute) – Erik Funkenbusch Oct 12 '18 at 21:15
  • Yea I created something like IEnumerable CountryList CountryList{set; get;} & inside country list is the enum – authy Oct 12 '18 at 21:18
  • Once again, IEnumerable is *NOT* an enum. DO NOT use IEnumerable with EnumDropDownListFor. What you're saying doesn't make any sense. What do you mean "inside country list is the enum"? An enum is already a collection of items, you wouldn't put it in another collection. – Erik Funkenbusch Oct 12 '18 at 21:19
  • I had so many problems with cannot pass Models.CountryList to IEnumerables that I'm now just trying to find to use List. So same way just made CountryList SelectedCountry {get; set;} also used Dropdownlist instead of enumDropDownList. In CountryList I used public List country { get; set; } I still have the issue of cannot convert CountryList to ienumerable. I'm having problems with both enum and list way. should I just create the country list as a basic string forthe dropdownlist? – authy Oct 12 '18 at 21:38
  • Now the probelm in Model.Country is System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Web.Mvc.WebViewPage.Model.get returned null. Btw the database is populated with country names. – authy Oct 12 '18 at 22:08

2 Answers2

0

I would sugest you to add a html extension method that implement dropdownlist for a generic enumarator. Take a look at this answer.

0

There is slightly change in your view where you bind the Country

this is actually you write

 @Html.EnumDropDownListFor(model => model.CountryList)

Change is need to add name property in your country list (it must be ID that is your taken in your model) , so it must be like this to maintain country list value

@Html.DropDownListFor(model => model.CountryList.Id, new SelectList(model => model.CountryList)

Becouse every HTML control need unique indentifier

Community
  • 1
  • 1