0

For some reason my SelectedId in my dropdown is always null when I post back the form. The items in the model are not null, just the SelectedID. All of the other values for my model are fine except for the drop down selection. Here is all the code related to my dropdown. Any ideas? Thank you!

P.S - I am using a template for the dropdown and the dropdowns are created automatically from my model when I use @Html.EditorForModel() in my cshtml view.

Template:

@model AFUEMVC.Models.DropDownModels.DropDownViewModel
@Html.DropDownListFor(x => x.SelectedId,  new SelectList(Model.Items, "Value", "Text",      Model.SelectedId))

Model:

//Dropdowns
[Display(Name = "Fire Type"),]
public DropDownViewModel FireTypeItems { get; set; }

ViewModel:

    public class DropDownViewModel
{
    public string SelectedId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

Edit: I am filling my dropdown the following way.

 public void GenerateDropDowns(BasicIdentificationModel model)
    {
        ///////////////////////DROP DOWNS 
        model.FireTypeItems = GetFireTypeItems(); 
        /////////////////////////////////////

    }

 public DropDownViewModel GetFireTypeItems()
    {
        DropDownViewModel newdd = new DropDownViewModel();

        newdd.Items = new[]
            {
                new SelectListItem { Value = "1", Text = "IA" },
                new SelectListItem { Value = "2", Text = "Extended Attack" },
                new SelectListItem { Value = "3", Text = "Large Fire Support" },                  
            };

        return newdd;

    }

  [HttpPost]
    public ActionResult BasicIdentificationIndex(BasicIdentificationModel returndata)
    {
        GenerateDropDowns(returndata);             
        SaveDB(returndata);       
       return RedirectToAction("DispatchResponseIndex", "DispatchResponse");
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
John Edwards
  • 1,536
  • 3
  • 14
  • 33

2 Answers2

1

Solved.

@Html.DropDownListFor(model => model.State, new SelectList(Enum.GetValues(typeof(MyNamespace.Enums.States))))

I found a post here:

MVC3 Razor DropDownListFor Enums

By far the easiest way to do this.

Community
  • 1
  • 1
John Edwards
  • 1,536
  • 3
  • 14
  • 33
0

Try this (without using templates):

Model:

public class myDbContext : DbContext
{
    ...
    public DbSet<SelectListItem> SelectListItems{ get; set; }
    ...
    [Display(Name = "Fire Type"),]
    public string SelectedID { get; set; }
    ...
}

In your View:

@Html.DropDownListFor(model => model.SelectedID, new SelectList(new Models.myDbContext().SelectListItems, "Value", "Text"))                
Amin Saqi
  • 18,549
  • 7
  • 50
  • 70
  • I get an exception with @Html.DropDownListFor(model => model.SelectedID, new SelectList(new AFUEMVC.Models.BasicIdentificationModel().SelectListItems, "Value", "Text")) One or more validation errors were detected during model generation: \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'SelectListItem' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'SelectListItems' is based on type 'SelectListItem' that has no keys defined. – John Edwards May 29 '13 at 19:29
  • That's because your entity class has no primary key defined. Open your model .edmx file. Right-click on the SelectedId property and choose properties. set the Entity Key property to true. – Amin Saqi May 29 '13 at 19:36
  • I am a little confused as to why I am doing all of this. One of my requirements is to have the dropdown items client side(as they eventually want to be able to use it offline with html 5 cached web app). So even if it works like this, it doesn't seem to really accomplish anything, though I truly appreciate the help. – John Edwards May 29 '13 at 19:44