Asp.net MVC验证未启动
我正在使用ASP.NETMVC2。我的表单验证没有启动。我想不出这里出了什么问题 型号: 视图: 您应该在web配置中将ClientValidationEnabled设置为true,或者在使用BeginForm之前添加以下代码Asp.net MVC验证未启动,asp.net,asp.net-mvc,asp.net-mvc-2,Asp.net,Asp.net Mvc,Asp.net Mvc 2,我正在使用ASP.NETMVC2。我的表单验证没有启动。我想不出这里出了什么问题 型号: 视图: 您应该在web配置中将ClientValidationEnabled设置为true,或者在使用BeginForm之前添加以下代码 <% Html.ClientValidationEnabled = true; %> 最后,我让服务器端验证工作正常。 添加对UpdateModel或TryUpdateModel的调用,该调用将填充您的模型并触发验证规则,以正确设置ModelState.I
<% Html.ClientValidationEnabled = true; %>
最后,我让服务器端验证工作正常。 添加对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();
}
}
只有当您的操作接受模型对象作为参数时,MVC2的内置服务器端验证才会起作用。这会导致MVC创建模型对象,并自动将传入的表单输入值映射到该对象。作为此过程的一部分,它还将检查模型的DataAnnotation验证属性是否有效。如果所有内容都有效,那么代码中的ModelState.IsValid检查将返回true
当您的操作接受FormCollection时,不会发生这种情况,因此,不会计算DataAnnotation验证属性,也不会设置ModelState.IsValid。我们可以通过自己执行这些步骤来纠正这个问题。MVC2提供了一些函数,我们可以使用它们来完成这个UpdateModel和TryUpdateModel。不同之处在于,如果存在验证错误,Update将抛出,而TryUpdate将简单地将IsValid设置为false
使用UpdateModel的另一个好处是,将使用表单集合中的所有值填充空模型。获取一个错误,该错误不包含ClientValidationEnabled的定义。这意味着您的项目中未包含MicrosoftMvcJqueryValidation。请参阅
<%@ 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>
<% Html.ClientValidationEnabled = true; %>
[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();
}
}