开发者

MVC3 view not updating using EF 4.1

开发者 https://www.devze.com 2023-04-10 12:56 出处:网络
I am having a problem updating a record.For some reason it is not even hitting the post action in the controller and just returning:

I am having a problem updating a record. For some reason it is not even hitting the post action in the controller and just returning: "An item with the same key has already been added."

It seems to be behaving as if it is doing an insert rather than an update. I would appreciate a new set of eyes on this. It is probably something very simple that I have missed.

Controller:

// GET: /Manage/Regions/Edit/5

    public ActionResult Edit(int id)
    {
        Region_CU regionEdit = (from r in db.Venues_Regions
                                where r.RegionsID == id
                                select new Region_CU
                                {
                                        RegionsID = r.RegionsID,
                                        Name = r.Name,
                                        CalendarLink = r.CalendarLink,
                                        MapIcon = r.MapIcon,
                                        QtrStart = r.QtrStart,
                                        QtrEnd = r.QtrEnd,
                                        FacebookLikeBox = r.FacebookLikeBox,
                                        FacebookId = r.FacebookId
                                    //  region = r 
                                }).Single();
        return View(regionEdit);
    }

    //
    // POST: /Manage/Regions/Edit/5

    [HttpPost]
    public ActionResult Edit(Region_CU r)
    {
        if (ModelState.IsValid)
        {
            var v = db.Venues_Regions.First(i => i.RegionsID == r.RegionsID);
                //v.RegionsID = r.RegionsID;
                v.Name = r.Name;
                v.CalendarLink = r.CalendarLink;
                v.MapIcon = r.MapIcon;
                v.QtrStart = r.QtrStart;
                v.QtrEnd = r.QtrEnd;
                v.FacebookLikeBox = r.FacebookLikeBox;
                v.FacebookId = r.FacebookId;
            //Venues_Regions v = new Venues_Regions
            //{
            //    RegionsID = r.RegionsID,
            //    Name = r.Name,
            //    CalendarLink = r.CalendarLink,
            //    MapIcon = r.MapIcon,
            //    QtrStart = r.QtrStart,
            //    QtrEnd = r.QtrEnd,
            //    FacebookLikeBox = r.FacebookLikeBox,
            //    FacebookId = r.FacebookId
            //};
            //db.Venues_Regions.Attach(v);
            //db.ObjectStateManager.ChangeObjectState(v, EntityState.Modified);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(r);
    }

View :

@model THPT_Razor.Areas.Manage.Models.Region_CU

@{
ViewBag.Title = "Edit Region";
}

<h2>Edit</h2>
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">   </script>
<script>
   $(document).ready(function () { $('.date').datepicker({ dateFormat: "mm/dd/yy"   });    });
</script>
@using (Html.BeginForm()) {
  @* @Html.ValidationSummary(true)*@
<fieldset>
    <legend>@Html.DisplayFor(model => model.Name)</legend>

    @Html.HiddenFor(model => model.RegionsID)
    @Html.HiddenFor(model => model.Name)
    @Html.HiddenFor(model => model.CalendarLink)
    <div class="editor-label">
        @Html.Label("Map Icon")
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.MapIcon, new SelectList(Model.mapicons,"id","Description"))
    </div>
    <div class="editor-label">
        @Html.Label("Quarter Start")
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.QtrStart, new { @class = "date" })
        @Html.ValidationMessageFor(model => model.QtrStart)
    </div>

    <div class="editor-label">
        @Html.Label("Quarter End")
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.QtrEnd, new { @class = "date" })
        @Html.ValidationMessageFor(model => model.QtrEnd)
    </div>

    <div class="editor-label">
        @Html.Label("Region Facebook ID")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FacebookId)
        @Html.ValidationMessageFor(model => model.FacebookId)
    </div>
    <div class="editor-label">
       @Html.Label("Region Facebook LikeBox Code")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FacebookLikeBox)
        @Html.ValidationMessageFor(model => model.FacebookLikeBox)
    </div>
    <p>
        <input type="submit" value="Update" />
    </p>
</fieldset>

Edit:

I have received several good suggestions but I guess I have not been clear. Region_CU is not an Entity. Venues_Regions is what I am trying to update. see the comments in the class below for clarification. The original objective was to build a simple wrapper that had the Venues_Regions object and a list object for the map icons. However, the data annotation for the field likebox was not being passed through resulting in the the Venues_Regions object to be broken out. Now when I try to save the update it is not even hitting the http post action. I hope this clears up what I am trying to accomplish and asking for help with. Thanks again for all the help and quick responses.

//create and update
public class Region_CU
{

    public Region_CU()
    {

    }
    public List<MapIcon> mapicons { get; set; }
    //public Venues_Regions region { get; set; }

    // The fields below are what makes up Veunes_Region
    // this was brok开发者_C百科en out from the above Venues_Region 
    // because the UIHint was not being passed through
    public int RegionsID { get; set; }
    public string Name { get; set; }
    public string CalendarLink { get; set; }
    public int MapIcon { get; set; }
    public DateTime? QtrStart { get; set; }
    public DateTime? QtrEnd { get; set; }

    [UIHint("tinymce_jquery_full"), AllowHtml]
    public string FacebookLikeBox { get; set; }
    public string FacebookId { get; set; }
    public string mapIcon { get; set; }

}

Edit #2: After a good nights sleep the solution presented itself. In the update action all I needed to do was change from the wrapper being passed in to the Venues_Region object being passed in and now everything works.

Thanks for all the help and suggestions.

Thanks in advance for the help, Chris


Actually all you need to do is load the venues and call TryUpdateModel. You don't even need to pass in the object. Then save the venue. Another approach is to use automapper to copy the fields between objects or use the attach method as mentioned but either way no manual field copying is required.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号