0
我正在處理MVC POST/GET請求,但我觀察到一些奇怪的行爲。爲什麼我的表單不能正確發佈?
我的表單在通過點擊瀏覽器中的「保存」進行編輯後發佈,但是當我收到請求後,所有屬性都未定義。 CourseName
,CourseCreditPts
,Institute
爲零或零。
代碼:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(tblCourse a_Course)
{
// Retrieve existing object
var CourseToBeSaved = _pr.FindCourseById(a_Course.CourseID);
// without try-catch exception thrown
try
{
TryUpdateModel(CourseToBeSaved);
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
// Save
_pr.Save();
return View();
}
,幷包含代碼.aspx
文件:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<DataWarehouse.tblCourse>" %>
<%@ Import Namespace="DataWarehouse.Helpers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit Course</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<%: Html.HiddenFor(x=>x.CourseID) %>
<table>
<tr>
<td><b>Institute</b></td>
<td><%: Html.DropDownList("InstituteID")%> <%: Html.ValidationMessageFor(x=>x.InstituteID) %></td>
</tr>
<tr>
<td><b>CourseName</b></td>
<td><%: Html.TextBox("EditAbleCourseName")%> <%: Html.ValidationMessageFor(x => x.CourseName)%></td>
</tr>
<tr>
<td><b>CreditPoints</b></td>
<td><%: Html.TextBox("EditAbleCreditPoints")%> <%: Html.ValidationMessageFor(x => x.CreditPts)%></td>
</tr>
<tr>
<td><b>Status</b></td>
<td><%: Html.DropDownList("EditAbleStatus")%> <%: Html.ValidationMessageFor(x => x.Status)%></td>
</tr>
</table>
<p>
<input type="submit" value="Save" />
</p>
<% } %>
</asp:Content>
這個問題就解決了。但在這期間出現了另一個問題。 CourseID(主鍵值不能從瀏覽器的POST請求中正確傳輸)並且CourseID爲0.相反,它應該與當前編輯過程中的情況類似(必須爲136)。由於我必須使用此ID更新模型重定向頁面。 – Usman 2012-01-15 12:38:43