locked
Edit method not working RRS feed

  • Question

  • User-618475340 posted

    I created an MVC5 controller with views, using entity framework.  It displays the data fine, the 'details' method works ok, so does both the  'delete' and 'create new' methods.  However, when I go to edit a record, it displays the form with the current as expected data, but when I click the Save button there is some activity, I think it's a postback but not sure, but then it stays on the same page and the data doesn't get updated in the database.  There is no compilation error, and it doesn't throw an exception either.  This is the method in the controller

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include = "venueid,venuename,contactname,address1,address2,city,county,phone1,phone2,weburl,email")] venues venues)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(venues).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(venues);
            }

    This is the last field in the view and the button

            <div class="form-group">
                @Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>

    and finally here is the model

        [Table("venues")]
        public partial class venues
        {
            [Key]
            public int venueid { get; set; }
            [Required]
            [Display(Name = "Venue")]
            public string venuename { get; set; }
            [Display(Name = "Contact")]
            public string contactname { get; set; }
            [Required]
            [Display(Name = "Address 1")]
            public string address1 { get; set; }
            [Display(Name = "Address 2")]
            public string address2 { get; set; }
            [Required]
            [Display(Name = "City")]
            public string city { get; set; }
            [Display(Name = "County")]
            public string county { get; set; }
            [Required]
            [Display(Name = "Postcode")]
            public string postcode { get; set; }
            [Display(Name = "Phone 1")]
            public string phone1 { get; set; }
            [Display(Name = "Phone 2")]
            public string phone2 { get; set; }
            [Display(Name = "Website")]
            public string weburl { get; set; }
            [Display(Name = "Email")]
            public string email { get; set; }
    
            public List<venueaccass> venueassigns { get; set; }
        }
    

    I have an almost identical edit method which uses a different model, but that works fine.  What am I missing?

    Thursday, May 21, 2015 9:45 AM

Answers

  • User-618475340 posted

    Ha, fixed it. I'd missed out the postcode field from the controller and the view, a field which cannot be null.  I would have thought it would have thrown a SQL Server exception but it never.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 22, 2015 7:52 AM

All replies

  • User1711366110 posted

    public ActionResult Edit([Bind(Include = "venueid,venuename,contactname,address1,address2,city,county,phone1,phone2,weburl,email")] venues venues) {

       As per this line, the issue might be the name collision of "venues" are repeated in ActionResult. so you can try like below :

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "venueid,venuename,contactname,address1,address2,city,county,phone1,phone2,weburl,email")] 
    venues venue) { if (ModelState.IsValid) { db.Entry(venue).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(venue); }
    Friday, May 22, 2015 5:04 AM
  • User-618475340 posted

    No, that didn't work, it's having the same effect, or lack of effect more accurately.  Could it be that the working version with a different table, "Bands", is based on an ADO.NET entity data model - bands.edmx, whereas the venues model was just hand coded?

    Friday, May 22, 2015 7:37 AM
  • User-618475340 posted

    Ha, fixed it. I'd missed out the postcode field from the controller and the view, a field which cannot be null.  I would have thought it would have thrown a SQL Server exception but it never.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 22, 2015 7:52 AM