MVC3视图不使用EF 4.1更新

时间:2011-10-05 03:43:59

标签: asp.net-mvc-3 entity-framework-4 razor

我在更新记录时遇到问题。出于某种原因,它甚至没有击中控制器中的post动作并且只是返回: “已经添加了具有相同键的项目。”

它似乎表现得像是在进行插入而不是更新。我很欣赏这一个新的眼睛。我错过了很简单的事情。

控制器:

// 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);
    }

查看:

@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>

编辑:

我收到了一些很好的建议,但我想我还不清楚。 Region_CU不是实体。 Venues_Regions就是我想要更新的内容。请参阅下面的课程中的评论以获得澄清。最初的目标是构建一个包含Venues_Regions对象和地图图标列表对象的简单包装器。但是,字段likebox的数据注释未被传递,导致Venues_Regions对象被分解。现在,当我尝试保存更新时,它甚至没有点击http post动作。我希望这能清除我想要完成的任务并寻求帮助。再次感谢所有帮助和快速回复。

//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 broken 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; }

}

编辑#2: 经过一夜好眠,解决方案呈现出来。在更新操作中,我需要做的就是从传入的包装器更改为传入的Venues_Region对象,现在一切正常。

感谢所有的帮助和建议。

提前感谢您的帮助, 克里斯

1 个答案:

答案 0 :(得分:0)

实际上你需要做的就是加载场地并调用TryUpdateModel。你甚至不需要传入对象。然后保存场地。另一种方法是使用automapper复制对象之间的字段或使用所提到的attach方法,但无论如何都不需要手动字段复制。