問候。從視圖中刪除操作頁面
爲了讓我的頭繞過MVC,我剛剛實現了一個簡單的添加/刪除頁面,它也查看記錄。這使得用戶可以更輕鬆地從一個頁面執行大部分操作,而無需離開以執行簡單的任務。該頁面可讓您添加記錄(具有ID和名稱的公司),該記錄工作正常。但是,當我刪除一條記錄時,我做了以下操作以允許刪除發生:
<%= Html.ActionLink("delete", "DeleteBusiness", new { businessToDelete = B.BusinessID }) %>
這很好地刪除記錄。這是我的控制器動作看起來像它:
public ActionResult DeleteBusiness(string businessToDelete)
{
try
{
if (!ModelState.IsValid)
return View("Businesses", _contractsControlRepository.ListBusinesses());
_contractsControlRepository.DeleteBusiness(businessToDelete);
return View("Businesses", _contractsControlRepository.ListBusinesses());
}
catch
{
return View("Businesses", _contractsControlRepository.ListBusinesses());
}
}
從企業的網頁我有這只是引導我在做的工作,然後控制器這個動作的ActionLink的
所以返回我以前的觀點在。那麼問題是,一旦我刪除記錄我的實際URL結束這樣的:
http://localhost:3340/Accounts/ContractsControl/DeleteBusiness?businessToDelete=TEST
這是不好的,因爲當我然後去添加新的記錄,它不會讓我。我的控制器動作,它可以讓我再補充記錄是這樣的:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Businesses(Business business)
{
try
{
if (!ModelState.IsValid)
return View(_contractsControlRepository.ListBusinesses());
_contractsControlRepository.CreateBusiness(business);
return View(_contractsControlRepository.ListBusinesses());
}
catch
{
return View(_contractsControlRepository.ListBusinesses());
}
}
任何post
請求被視爲創造新的記錄。它是否正確?這裏是我的觀點只是最糟糕的是:
<% using (Html.BeginForm())
{ %>
<fieldset class="inline-fieldset">
<legend>Add Business</legend>
<p>
<label for="ID">ID</label>
<%= Html.TextBox("BusinessID", null, new { style = "width:50px;", @class = "uppercase", maxlength = "4" })%>
<%= Html.ValidationMessage("BusinessID", "*")%>
<label for="Business">Business</label>
<%= Html.TextBox("BusinessCol")%>
<%= Html.ValidationMessage("BusinessCol", "*")%>
<input type="submit" value="Add" />
</p>
<%= Html.ValidationSummary("Uh-oh!") %>
</fieldset>
<% } %>
<table>
<tr>
<th>ID</th>
<th>Business</th>
<th></th>
<th></th>
</tr>
<% foreach (Business B in ViewData.Model)
{ %>
<tr>
<td><%= B.BusinessID %></td>
<td><%= B.BusinessCol %></td>
<td class="edit"><%= Html.ActionLink("edit", "EditBusiness", new { id = B.BusinessID }) %></td>
<td class="delete"><%= Html.ActionLink("delete", "DeleteBusiness", new { businessToDelete = B.BusinessID }) %></td>
</tr>
<% } %>
</table>
正如你可以在最下方,我有行動鏈接查看(忽略編輯之一)。所以,如果我刪除一條記錄,然後立即嘗試添加一條記錄,它將無法正常工作,因爲(我假設)該URL是錯誤的。我在這裏做錯了什麼?
FIXED
public RedirectToRouteResult DeleteBusiness(string businessToDelete)
{
try
{
if (!ModelState.IsValid)
return RedirectToAction("Businesses");
_contractsControlRepository.DeleteBusiness(businessToDelete);
return RedirectToAction("Businesses");
}
catch
{
return RedirectToAction("Businesses");
}
}
賓果。我會更新我的問題以顯示修復程序。 – Kezzer 2009-07-30 10:43:36