DropDownList - MVC3 Razor Application
-
Using EF Code First, I have implemented a dropdownlist on a create method everything works fine however duplicate records are posted as the method is called twice. Any idea what I have not implemented correcty. Model(s) public class Country { public int CountryId { get; set; } public string Name { get; set; } } public class State { public int StateId { get; set; } [Required(ErrorMessage = " Name is required")] public string Name { get; set; } [Required(ErrorMessage = " Code is required")] public string Code { get; set; } public virtual Country Country { get; set; } [Required(ErrorMessage = " The Country is required")] public int CountryId { get; set; } } Controller (partial) [HttpPost] public ActionResult Create(State newState) { ViewData["Countries"] = db.Countries.ToList(); try { // TODO: Add insert logic here db.States.Add(newState); db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(newState); } } View (Partial) <div class="editor-label"> @Html.LabelFor(model => model.CountryId) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.CountryId, new SelectList(ViewBag.Countries, "CountryId", "Name")) @Html.ValidationMessageFor(model => model.CountryId) </div> Regards
dotman1
-
Using EF Code First, I have implemented a dropdownlist on a create method everything works fine however duplicate records are posted as the method is called twice. Any idea what I have not implemented correcty. Model(s) public class Country { public int CountryId { get; set; } public string Name { get; set; } } public class State { public int StateId { get; set; } [Required(ErrorMessage = " Name is required")] public string Name { get; set; } [Required(ErrorMessage = " Code is required")] public string Code { get; set; } public virtual Country Country { get; set; } [Required(ErrorMessage = " The Country is required")] public int CountryId { get; set; } } Controller (partial) [HttpPost] public ActionResult Create(State newState) { ViewData["Countries"] = db.Countries.ToList(); try { // TODO: Add insert logic here db.States.Add(newState); db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(newState); } } View (Partial) <div class="editor-label"> @Html.LabelFor(model => model.CountryId) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.CountryId, new SelectList(ViewBag.Countries, "CountryId", "Name")) @Html.ValidationMessageFor(model => model.CountryId) </div> Regards
dotman1
1. Use PRE tags to format the code part. It makes the question readable. 2. Did you try to use VS Debugger and see how the execution is happening? Based on what you have posted, it looks ok.
Sandeep Mewara [My latest tip/trick] [Forum guidelines]
-
1. Use PRE tags to format the code part. It makes the question readable. 2. Did you try to use VS Debugger and see how the execution is happening? Based on what you have posted, it looks ok.
Sandeep Mewara [My latest tip/trick] [Forum guidelines]
Thanks for the input. Yes I did step through using VS debugger the create method is called and process the steps through to and including the call to db.SaveChanges(); return RedirectToAction("Index"); is not reached and the create method is called processing the second post this time after the call to db.SaveChanges(); return RedirectToAction("Index"); is returned. This is result is random in other instances the process flow is as expected but once again the create method is called twice.
dotman1