且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

表单模型在POST之前未通过验证

更新时间:2023-12-01 20:44:04

当您单击提交"按钮时,它会将表单发布到服务器上,在该服务器上您的操作方法具有用于检查表单是否有效的代码(ModelState.IsValid) .这是服务器端验证.必须将表单发布到服务器上.

When you click on the submit button, it posts the form to the server where your action method has code to check whether the form is valid ( ModelState.IsValid). This is server side validation. The form has to be posted to server for this to happen.

如果您希望在客户端进行验证(如果验证失败则不要提交表单),则需要确保已将相关的javascript库/文件加载到页面上.

If you want to the validation to happen at client side (and not submit the form if the validation fails), you need to make sure that you have the relevant javascript libraries/files loaded to your page.

  1. jquery.validate.js
  2. jquery.validate.unobtrusive.js

您可以将它们包括在布局文件或特定视图中.如果是特定视图,请确保将其包含在按揭"部分中.

You may include those in your layout file or your specific view. If it is the specific view, make sure you include those in the Scrips section.

@section Scripts
{
 <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
 <script src="~/lib/jquery-validation- unobtrusive/jquery.validate.unobtrusive.js"></script>
}

更新文件的路径,以使其与项目中这些文件的位置匹配.您也可以使用该位置访问公共CDN.

Update the path of the files to match with where you have those files in your project. You may also use the location to a public cdn.