2

I would like to get parameter from dropdownList on html page and send it to my controller, create new Model object, and insert it into database.

it's my controller (two methods to create My_Model):

public ActionResult Create()
    {
        IEnumerable<MusicStyle> musicStyleList = db.MusicStyles.ToList();
        ViewData["musicStyles"] = new SelectList(musicStyleList);
        return View();
    }

    // POST: Bands/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "id,name,info,appearanceDate,musicStyles")] Band band)
    {
        IEnumerable<MusicStyle> musicStyleList;
        if (ModelState.IsValid)
        {
            musicStyleList = db.MusicStyles;
            ViewData["musicStyles"] = new SelectList(musicStyleList).ToList();
            db.Bands.Add(band);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        musicStyleList = db.MusicStyles;
        ViewData["musicStyles"] = new SelectList(musicStyleList).ToList();
        return View(band);
    }

it's my dropdownList on html page:

@Html.DropDownList("musicStyles", "select style")

it's my model:

 public class Band
{
    [Required]
    public int id { get; set; }

    [Required]
    [RegularExpression(@"^[a-zA-Z-\s\\\/*_]+$")]
    [StringLength(60, MinimumLength = 1)]
    public string name { get; set; } 

    public string info { get; set; }

    [Required]
    [Display(Name = "Appearance date")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime appearanceDate { get; set; }

    public List<MusicStyle> musicStyles { get; set; }
    public List<Song> songs { get; set; }
    public List<Album> albums { get; set; }
}

it has a List of musicStyles(reference many-to-many), and I want to set selected element from dropdownList to this model. But in result of Create method I have a null musicStyles, and ModelState.isValid == false

Ales
  • 170
  • 1
  • 4
  • 14

1 Answers1

3

A <select> only posts back a single value - it cant bind to property List<MusicStyle> musicStyles You need a view model with properties you can bind to

public class BandVM
{
  [Display(Name = "Music style")]
  [Required(ErrorMessage = "Please select a style")]
  public string SelectedMusicStyle { get; set; }
  public SelectList MusicStyleList { get; set; }
  ....
}

And in the controller

public ActionResult Create()
{
  BandVM model = new BandVM();
  model.MusicStyleList = new SelectList(db.MusicStyles);
  return View(model);
}

And in the view

@Html.LabelFor(m => m.SelectedMusicStyle)
@Html.DropDownListFor(m => m.SelectedMusicStyle, Model.MusicStyleList, "select style")
@Html.ValidationMessageFor(m => m.SelectedMusicStyle)

In the POST method

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(BandVM model) // NO Bind.Include!
{
  // model.SelectedMusicStyle will contain the selected value
  // Create a new instance of the data model and map the view model properties to it
  // Save and redirect
}