[英]ASP.NET MVC 2 UpdateModel() is not updating values in memory or database
我是MVC的新手,因此正在這里的NerdDinner教程中進行工作。 特別是,我在使用UpdateModel方法時遇到了問題,該教程的第五部分對此進行了說明。 問題是,當我嘗試使用UpdateModel方法編輯晚餐對象的值時,這些值不會得到更新,並且不會引發任何異常。
奇怪的是,本教程中說明的“創建”或“刪除”功能沒有任何問題。 僅更新功能不起作用。
下面,我包括了我正在使用的Controller代碼以及視圖標記,該標記包含在aspx視圖文件和ascx部分視圖文件中。
這是我的控制器內的代碼,名為DinnerController.cs:
//
// GET: /Dinners/Edit/2
[Authorize]
public ActionResult Edit(int id)
{
Dinner dinner = dinnerRepository.GetDinner(id);
return View(new DinnerFormViewModel(dinner));
}
//
// POST: /Dinners/Edit/2
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner(id);
try
{
UpdateModel(dinner);
var x = ViewData.GetModelStateErrors(); // <-- to catch other ModelState errors
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinner.GetRuleViolations());
return View(new DinnerFormViewModel(dinner));
}
}
從另一個StackOverflow線程中讀取可能的解決方案后,添加了帶有注釋“捕獲其他ModelState錯誤”的行,這里:
ASP.NET MVC Updatemodel不更新但不引發錯誤
不幸的是,該解決方案並沒有幫助我。
這是我的Dinners / Edit.aspx視圖中的相應標記:
<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit Dinner</h2>
<% Html.RenderPartial("DinnerForm"); %>
</asp:Content>
這是我的DinnerForm.ascx部分視圖中的相應標記。 此部分視圖文件也被創建功能使用,可以正常工作 :
<%=Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<fieldset>
<p>
<label for="Title">Dinner Title:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Title)%>
<%=Html.ValidationMessage("Title", "*") %>
</p>
<p>
<label for="EventDate">EventDate:</label>
<%=Html.TextBoxFor(model => Model.Dinner.EventDate, new { value = String.Format("{0:g}", Model.Dinner.EventDate) })%>
<%=Html.ValidationMessage("EventDate", "*") %>
</p>
<p>
<label for="Description">Description:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Description)%>
<%=Html.ValidationMessage("Description", "*")%>
</p>
<p>
<label for="Address">Address:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Address)%>
<%=Html.ValidationMessage("Address", "*") %>
</p>
<p>
<label for="Country">Country:</label>
<%=Html.DropDownListFor(model => Model.Dinner.Country, Model.Countries)%>
<%=Html.ValidationMessage("Country", "*") %>
</p>
<p>
<label for="ContactPhone">ContactPhone #:</label>
<%=Html.TextBoxFor(model => Model.Dinner.ContactPhone)%>
<%=Html.ValidationMessage("ContactPhone", "*") %>
</p>
<p>
<label for="Latitude">Latitude:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Latitude)%>
<%=Html.ValidationMessage("Latitude", "*") %>
</p>
<p>
<label for="Longitude">Longitude:</label>
<%=Html.TextBoxFor(model => Model.Dinner.Longitude)%>
<%=Html.ValidationMessage("Longitude", "*") %>
</p>
<p>
<input type="submit" value="Save"/>
</p>
</fieldset>
<% } %>
無論如何,我已經為此花了幾個小時了,而且我沒有主意。 因此,我希望這里有人可以幫助我朝着正確的方向前進,以便弄清我做錯了什么。
dinnerRepository.Save()
是實際更新數據庫的代碼。 UpdateModel(dinner)
作用是從表單集合中提取值並將其放入dinner
對象中。
你把事情弄混了。 您正在將DinnerFormViewModel發送給View,但試圖接收Dinner。請按以下方式更改post方法:
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Edit(int id, FormCollection formValues)
{
var dinner=new DinnerFormViewModel(dinnerRepository.GetDinner(id));
try
{
UpdateModel(dinner);
var x = ViewData.GetModelStateErrors(); // <-- to catch other ModelState errors
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.Dinner.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinner.GetRuleViolations());
return View(new DinnerFormViewModel(dinner));
}
}
我可能在這里錯過了一些事情,現在不記得晚餐了。 請檢查那些
編輯:實際上我意識到這篇文章並不能真正解決問題。 問題中發布的代碼對我有用。 有問題,但不是這里。
以防萬一將來對其他人有幫助,這里的問題不一定是由於我懷疑使用了DinnerFormViewModel。 相反,問題出在使用強類型的輔助方法(例如Html.TextBoxFor)以及我調用UpdateModel方法的方式。
這個問題,它的解決方案進行了詳細的StackOverflow的另一個線程解釋說, 這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.