我在更新記錄時遇到問題。由於某些原因,它甚至沒有觸及控制器中的後置動作並返回: 「已添加具有相同密鑰的項目。」未使用EF 4.1更新MVC3視圖
它似乎表現得好像在做插入而不是更新。我很欣賞這一套新的眼睛。這可能很簡單,我錯過了。
控制器:
// 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對象的更改,現在一切正常。
感謝您的所有幫助和建議。
在此先感謝您的幫助, 克里斯
你有沒有考慮搬到EF 4.1?它的DbContext API使得更容易更新任何你需要的東西。 – tugberk