我正在使用ASP.NET MVC 2.我的表单验证没有触发。我不知道这里有什么不对。
型号:
public class Stock
{
public int StockID { get; set; }
[Required(ErrorMessage = "Please Select a Client")]
public int ClientID { get; set; }
[Required(ErrorMessage = "Please Select An Item")]
public int ItemID { get; set; }
[Required(ErrorMessage = "Please Enter Item Count")]
public int ItemCount { get; set; }
[Required(ErrorMessage = "Please Enter Item Price")]
public double Price { get; set; }
[Required(ErrorMessage = "Please Enter Other Expences")]
public double OtherExpences { get; set; }
public double TotalStockValue { get; set; }
[Required(ErrorMessage = "Please Enter Delivery date")]
public DateTime DeliveryDate { get; set; }
public string Description { get; set; }
public List<Client> lstClient { get; set; }
public IEnumerable<Item> lstItem { get; set; }
public string ClientName { get; set; }
public string ItemName { get; set; }
}
控制器:
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
if (ModelState.IsValid)
{
StockRepository rep = new StockRepository();
Stock stock = new Stock();
stock.ClientID =Convert.ToInt32(Request.Form["ClientID"]);
stock.DeliveryDate =Convert.ToDateTime(Request.Form["DeliveryDate"]);
stock.Description = Request.Form["Description"];
stock.ItemCount =Convert.ToInt32(Request.Form["ItemCount"]);
stock.ItemID =Convert.ToInt32(Request.Form["ItemID"]);
stock.Price =Convert.ToDouble(Request.Form["Price"]);
stock.OtherExpences = Convert.ToDouble(Request.Form["OtherExpences"]);
stock.TotalStockValue =Convert.ToDouble((stock.Price * stock.ItemCount)+stock.OtherExpences);
rep.Create(stock);
rep.Save();
return RedirectToAction("Index");
}
else
return View();
}
catch
{
return View();
}
}
查看:
<%@ Page Title="" Language="C#" MasterPageFile="~/StockMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<StockManagement.Models.Stock>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<link rel="stylesheet" href="<%= Url.Content("http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css") %>" />
<script src="<%= Url.Content("http://code.jquery.com/jquery-1.9.1.js") %>"></script>
<script src="<%= Url.Content("http://code.jquery.com/ui/1.10.3/jquery-ui.js") %>"></script>
<script>
$(function () {
$("#DeliveryDate").datepicker();
});
</script>
<h2>Create</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.Label("Select Client")%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(x => x.ClientID, new SelectList(Model.lstClient, "ClientID", "Name"), "-- Please Select a Client --") %>
<%: Html.ValidationMessageFor(model => model.ClientID)%>
</div>
<div class="editor-label">
<%: Html.Label("Select Item") %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(x => x.ItemID, new SelectList(Model.lstItem, "ItemID", "ItemName"), "-- Please Select an Item --")%>
<%: Html.ValidationMessageFor(model => model.ItemID)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ItemCount) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ItemCount) %>
<%: Html.ValidationMessageFor(model => model.ItemCount) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Price) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Price) %>
<%: Html.ValidationMessageFor(model => model.Price) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.OtherExpences) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.OtherExpences) %>
<%: Html.ValidationMessageFor(model => model.OtherExpences) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DeliveryDate) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DeliveryDate) %>
<%: Html.ValidationMessageFor(model => model.DeliveryDate) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.Description) %>
<%: Html.ValidationMessageFor(model => model.Description) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
答案 0 :(得分:0)
您应该在网络配置中将 ClientValidationEnabled 设置为 true ,或在使用BeginForm之前添加以下代码
<% Html.ClientValidationEnabled = true; %>
答案 1 :(得分:0)
最后,我得到了服务器端验证工作。 添加对UpdateModel或TryUpdateModel的调用,该调用将填充模型并激活验证规则,正确设置ModelState.IsValid属性。
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
Stock stock = new Stock();
TryUpdateModel(stock);
if (ModelState.IsValid)
{
StockRepository rep = new StockRepository();
stock.ClientID =Convert.ToInt32(Request.Form["ClientID"]);
stock.DeliveryDate =Convert.ToDateTime(Request.Form["DeliveryDate"]);
stock.Description = Request.Form["Description"];
stock.ItemCount =Convert.ToInt32(Request.Form["ItemCount"]);
stock.ItemID =Convert.ToInt32(Request.Form["ItemID"]);
stock.Price =Convert.ToDouble(Request.Form["Price"]);
stock.OtherExpences = Convert.ToDouble(Request.Form["OtherExpences"]);
stock.TotalStockValue =Convert.ToDouble((stock.Price * stock.ItemCount)+stock.OtherExpences);
rep.Create(stock);
rep.Save();
return RedirectToAction("Index");
}
else
return View();
}
catch
{
return View();
}
}
MVC 2的内置服务器端验证仅在您的操作接受Model对象作为参数时才有效。这会导致MVC创建模型对象并自动将传入的表单输入值映射到它。作为此过程的一部分,它还将检查模型的DataAnnotation验证属性是否有效。如果一切都有效,那么我们代码中的ModelState.IsValid检查将返回true。
当您的操作接受FormCollection时,这不会发生,因此永远不会评估DataAnnotation验证属性,并且永远不会设置ModelState.IsValid。我们可以通过简单地执行这些步骤来纠正这个问题。 MVC 2提供了我们可以用来执行此操作的函数(UpdateModel和TryUpdateModel)。如果存在验证错误,则Update将抛出,而TryUpdate只会将IsValid设置为false。
使用UpdateModel的另一个好处是,将使用表单集合中的所有值为您填充空模型。