Record not adding because ddl list not formatted correctly

smartelf

New Member
I was using this webpage: http://www.asp.net/mvc/tutorials/ge...ntity-framework-in-an-asp-net-mvc-applicationI am trying to use the following to create a new relational record, but nothing happens because the "ModelState.IsValid" isn't valid. When I removed the dropdownlist code and entered the genre manually on the view form, the record was added. So I'm either building the DDL wrong or displaying it wrong (i guess). Any help would be greatly appreciated. Thanks.viewmodel:\[code\]namespace MusicRental.ViewModels{ public class MusicGenresModel { public IEnumerable<Musics> Musics { get; set; } public IEnumerable<Genres> Genres { get; set; } }}\[/code\]controller: using MusicRental.ViewModels;\[code\] // GET: /Music/Create public ActionResult Create() { PopulateGenreDDL(); return View(); } // POST: /Music/Create [HttpPost] public ActionResult Create(Musics musics) { if (ModelState.IsValid) { db.Musics.Add(musics); db.SaveChanges(); return RedirectToAction("Index"); } PopulateGenreDDL(musics.parentGenre); return View(musics); } private void PopulateGenreDDL(object selectedGenre = null) { var genreQuery = from d in db.Genres orderby d.genreName select d; ViewBag.genreId = new SelectList(genreQuery, "genreId", "genreName", selectedGenre); }\[/code\]view:\[code\]<div class="editor-field"> @Html.DropDownList("genreId", (SelectList)ViewBag.genreId, "select one...") @Html.ValidationMessageFor(model => model.parentGenre.genreName)</div>\[/code\]
 
Top