c# - 验证 ASP.NET MVC 表单时的回发

标签 c# ajax asp.net-mvc

我使用 asp.net mvc 脚手架创建了一个 web 表单表单,如果没有回发,它就无法进行客户端验证。 [Required()] 正在回发,[EmailAddress] 验证器正在客户端进行验证。我使用 visual studio 2013 和带有 ef6 的 asp.net mvc 5。 这个 id 我的模型类:

namespace WebApplication4.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public partial class Tutor
{
    public Tutor()
    {
        this.Examinations = new HashSet<Examination>();
    }


    public decimal TutorID { get; set; }

    [Display(Name = "First Name ")]
    [Required(ErrorMessage = "Please Enter First Name.")]
    [DataType(DataType.Text)]
    public string FirstName { get; set; }

    [Display(Name = "Last Name ")]
    [Required(ErrorMessage = "Please Enter Last Name.")]
    [DataType(DataType.Text)]
    public string LastName { get; set; }

    [Display(Name = "Address Line 1 ")]
    [Required(ErrorMessage = "Please Enter Address Line 1.")]
    [DataType(DataType.Text)]
    public string Address1 { get; set; }

    [Display(Name = "Address Line 2 ")]
    [Required(ErrorMessage = "Please Enter Address Line 2.")]
    [DataType(DataType.Text)]
    public string Address2 { get; set; }

    [Display(Name = "Address Line 3 ")]
    public string Address3 { get; set; }

    [Display(Name = "Telephone 1 ")]
    [Required(ErrorMessage = "Please Enter Telephone No.")]
    [DataType(DataType.Text)]
    public string Tel1 { get; set; }

    [Display(Name = "Telephone 2 ")]
    [DataType(DataType.Text)]
    public string Tel2 { get; set; }

    [Display(Name = "Email Address")]
    [Required(ErrorMessage = "Please Enter E Mail Address.")]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    [DataType(DataType.EmailAddress)]
    public string EMail { get; set; }

    [Display(Name = "Password ")]
    [DataType(DataType.Password)]
    public string Password { get; set; }
    public Nullable<bool> IsConfirmed { get; set; }

     public virtual ICollection<Examination> Examinations { get; set; }
    }
}

这是我的 Controller 在 Controller 中的 Create() 方法:

    // GET: /Default1/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: /Default1/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include="TutorID,FirstName,LastName,Address1,Address2,Address3,Tel1,Tel2,EMail,Password,IsConfirmed")] Tutor tutor)
    {
        if (ModelState.IsValid)
        {
            db.Tutors.Add(tutor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(tutor);
    }

这是用于创建...的 View

  @model WebApplication4.Models.Tutor

  @{
     ViewBag.Title = "Create";
     Layout = "~/Views/Shared/_Layout.cshtml";
   }

   <h2>Create</h2>


   @using (Html.BeginForm()) 
    {
     @Html.AntiForgeryToken()

     <div class="form-horizontal">
      <h4>Tutor</h4>
      <hr />
      @Html.ValidationSummary(true)

      <div class="form-group">
        @Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address1, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address1)
            @Html.ValidationMessageFor(model => model.Address1)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address2, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address2)
            @Html.ValidationMessageFor(model => model.Address2)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address3, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address3)
            @Html.ValidationMessageFor(model => model.Address3)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Tel1, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Tel1)
            @Html.ValidationMessageFor(model => model.Tel1)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Tel2, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Tel2)
            @Html.ValidationMessageFor(model => model.Tel2)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.EMail, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EMail)
            @Html.ValidationMessageFor(model => model.EMail)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>
    </div>



    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
     @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我想验证客户端的所有内容。

最佳答案

确保正确加载 jquery.validate.js 库

BundleConfig.cs

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

在页面末尾

@Scripts.Render("~/bundles/jqueryval")

关于c# - 验证 ASP.NET MVC 表单时的回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551211/

相关文章:

asp.net-mvc - Web Api 调用在服务器上返回 404

c# - 在 DocumentDB 中查询满足特定条件的文档计数的最佳方法是什么?

c# - 还原 VS 2019 16.5.0 后找不到 Nuget 包

jquery - 如何在 AJAX 请求加载之前隐藏 div?

使用ajax加载页面并提交表单所需的java selenium解决方案

c# - 表单提交后 MVC 从 View 返回填充模型

c# - 使用 C# 将 shapefile(.shp) 转换为 SQL Server 2008?

c# - Asp.net Core 迁移,后操作不再有效

javascript - 如何在文档准备就绪时使用ajax加载图像

Javascript 执行 Action 以在 MVC4 中移动文件