[英]jquery validation in mvc3 application
有人可以幫助我在mvc3 html可編輯網格中進行驗證嗎? 列中值的總和不應超過一百。 我可以使用jQuery進行驗證還是可以進行服務器端驗證?
這是服務器端驗證
類:
public class Party
{
[Required(ErrorMessage = "Start date is required")]
public DateTime StartDate { get; set; }
[Required(ErrorMessage = "Duration is required")]
public int DurationInHours { get; set; }
[Required(ErrorMessage = "No. of joinees is required")]
[Range(2, 10, ErrorMessage = "No. of joinees should be minimum 2 and not more than 10")]
public int NoOfJoinees { get; set; }
public bool Drinks { get; set; }
}
控制器:
public class PartyController: Controller
{
public ActionResult Index()
{
return View();
}
}
視圖:
@model CustomValidation.MVC.Models.Party
@using (Html.BeginForm())
{
@Html.ValidationSummary()
Start date (MM/dd/yyyy HH:mm:ss AM/PM) *: @Html.TextBoxFor(x => x.StartDate, new { size = 25 })
Duration (Hours) *: @Html.DropDownListFor(x => x.DurationInHours, new[]{
new SelectListItem(){ Text = "1", Value = "1"},
new SelectListItem(){ Text = "2", Value = "2"},
new SelectListItem(){ Text = "3", Value = "3"},
new SelectListItem(){ Text = "4", Value = "4"},
new SelectListItem(){ Text = "5", Value = "5"}
}, "Select the duration", new { style = "width:180px" })
No. of joinees *: @Html.TextBoxFor(x => x.NoOfJoinees, new { size = 5 })
Drinks? @Html.CheckBoxFor(x => x.Drinks)
<input type="submit" value="Host the party!" />
}
和客戶端驗證:
的HTML
<input type="text" id="UserName" name="UserName"/>
<input type="button" onclick="Validation()" value="Enter" />
Javascript:
function Validation() {
var data= {
UserName: $('#UserName').val()
};
if (data.UserName.trim() == "" || data.UserName== undefined) {
$("#ShowWarning").html('<img src="/Image/warning.jpg" title="Please Enter UserName!">').show();
}
您也可以檢查以下示例
希望對您有幫助
在MVC中,您應始終使用客戶端和服務器端驗證。 如果用驗證屬性標記模型,則服務器端和客戶端驗證都應該可以正常工作。
請檢查此鏈接以獲取MVC3中的詳細驗證-http: //bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html
並在下面的Scott-Gu博客中進行檢查,這有助於進行驗證。
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.