Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. DropDownList - MVC3 Razor Application

DropDownList - MVC3 Razor Application

Scheduled Pinned Locked Moved ASP.NET
databasehtmlasp-net
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dotman1
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • D 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

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      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]

      D 1 Reply Last reply
      0
      • S Sandeep Mewara

        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]

        D Offline
        D Offline
        dotman1
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups