简体   繁体   English

mvc3应用程序中的jquery验证

[英]jquery validation in mvc3 application

Can someone help me to do validation in mvc3 html editable grid? 有人可以帮助我在mvc3 html可编辑网格中进行验证吗? The sum of the values on the column should not exceed one hundred. 列中值的总和不应超过一百。 Can I validate using jQuery or do server-side validation? 我可以使用jQuery进行验证还是可以进行服务器端验证?

This is Server Side Validation 这是服务器端验证

Class: 类:

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; }
}

Controller: 控制器:

public class PartyController: Controller
{
public ActionResult Index()
{
    return View();
}
} 

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!" />
}

And Client Side Validation: 和客户端验证:

HTML 的HTML

<input type="text" id="UserName" name="UserName"/>
<input type="button" onclick="Validation()" value="Enter" />

Javascript: 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();
}

Also you can check below example too 您也可以检查以下示例

http://www.mindstick.com/Articles/d17c1dc9-e00b-4c13-94e7-87dacdca027f/?Validation%20in%20ASP%20NET%20MVC3 http://www.mindstick.com/Articles/d17c1dc9-e00b-4c13-94e7-87dacdca027f/?Validation%20in%20ASP%20NET%20MVC3

i hope helps you 希望对您有帮助

In MVC, you should always use client side and server side validation. 在MVC中,您应始终使用客户端和服务器端验证。 If you mark the models with validation attributes both the server-side and client-side validation should work just fine. 如果用验证属性标记模型,则服务器端和客户端验证都应该可以正常工作。

Please check this link for detailed validation in MVC3 - http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html 请检查此链接以获取MVC3中的详细验证-http: //bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

And also check this below Scott-Gu blog which helps for validation. 并在下面的Scott-Gu博客中进行检查,这有助于进行验证。

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM