【问题标题】:jquery validation in mvc3 applicationmvc3应用程序中的jquery验证
【发布时间】:2013-09-28 19:04:27
【问题描述】:

有人可以帮我在 mvc3 html 可编辑网格中进行验证吗?列上的值的总和不应超过一百。我可以使用 jQuery 进行验证还是进行服务器端验证?

【问题讨论】:

    标签: jquery asp.net-mvc-3 validation


    【解决方案1】:

    这是服务器端验证

    类:

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

    你也可以查看下面的例子

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

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      在 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        • 1970-01-01
        • 2013-05-14
        相关资源
        最近更新 更多