我有一個簡單的在用戶輸入RSS源的地址的文本字段。如果該字段爲空我不會採取行動,這是我的標記:ASP.NET MVC簡單驗證我的表單失敗
<%=Html.ValidationSummary() %>
<table>
<tr>
<td>
Feed Url:
</td>
<td>
<%=Html.TextBox("url", null, new {@style="width:300px"}) %>
</td>
</tr></table>
我的控制也很簡單:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddFeed(FormCollection collection)
{
string url = collection.Get("url");
string roles = collection.Get("Roles");
if (string.IsNullOrEmpty(url))
{
ModelState.AddModelError("url", "Please provide a propre feed url");
}
if (string.IsNullOrEmpty(roles))
{
ModelState.AddModelError("Roles", "Please select a valid role");
}
if (ModelState.IsValid)
{
Session["url"] = url;
Session["Roles"] = roles;
return RedirectToAction("ValidateFeed");
}
else
{
return View();
}
}
當它失敗,將重新加載視圖,這使該行其中一個例外它呈現我的文本框,說有一個空指針異常。這確實botheres我來說,這應該是這麼簡單......但還是即時通訊掙扎
/H4mm3r
編輯請下來的我一直忽視的角色元素的下降,但是從標記刪除它爲簡單起見
試過了:-)沒有運氣:-(認爲「奧馬爾」在他的回答中有一個問題,現在就試試吧 – H4mm3rHead 2009-11-30 20:39:48