我正在使用ASP.NET MVC 2.我的表單驗證沒有解僱。我無法弄清楚這裏有什麼問題。MVC驗證沒有解僱
型號:
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>
得到一個錯誤「不包含定義ClientValidationEnabled」 – chamara 2013-05-04 07:15:19
那說明你還沒有包含在你的項目中MicrosoftMvcJqueryValidation。請參閱http://blogs.msdn.com/b/rickandy/archive/2009/10/03/client-side-validation-for-mvc-2-p2.aspx上的最佳可能場景 – DeveloperBuddy 2013-05-04 07:23:51