asp.net-mvc asp.net mvc 3 - ajax 表单提交和验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5200098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net mvc 3 - ajax form submit and validation
提问by santiagoIT
I am sorry if this has been asked already, but I have been looking for sometime but all I have found are rather old posts (mvc1, mvc2). I have a form which I would like to submit via Ajax.
如果这已经被问到,我很抱歉,但我一直在寻找一段时间,但我发现的都是相当旧的帖子(mvc1,mvc2)。我有一个表单,我想通过 Ajax 提交。
Thislooks like it would work but does not cover server side validation.
这看起来可以工作,但不包括服务器端验证。
1) I am unsure if I should use the AjaxHelper.BeginFormor use raw jquery calls ($.ajax)? What is the recommended approach here?
1) 我不确定我是应该使用AjaxHelper.BeginForm还是使用原始的 jquery 调用($.ajax)?这里推荐的方法是什么?
2) How do I handle client and server side validation? I am hoping the mvc framework provides a built in mechanism for dealing with this?
There are some validations which I am only doing server side. Would using a ValidationSummarystill work here?
2) 如何处理客户端和服务器端验证?我希望 mvc 框架提供一种内置机制来处理这个问题?有一些验证我只在服务器端进行。ValidationSummary在这里使用仍然有效吗?
I am using asp.net mvc3/razor with unobtrussive javascript validation.
我正在使用带有不显眼的 javascript 验证的 asp.net mvc3/razor。
Thank you!
谢谢!
Edit: (as requested by Bobby B below).This was added months after asking the question as a user wanted to know how to use AjaxHelper
编辑:(按照下面鲍比 B 的要求)。这是在提出问题几个月后添加的,因为用户想知道如何使用 AjaxHelper
This is the javascript code I used:
这是我使用的javascript代码:
<script type="text/javascript">
function ajaxValidate() {
return $('form').validate().form();
}
function getGbPostSuccess(ajaxContext){
// .... it is not necessary to do anything here.
}
function showFaliure(ajaxContext){
// handle failure
}
HTML snippet:
HTML 片段:
@using (Ajax.BeginForm("Index", "Home", new AjaxOptions
{
UpdateTargetId = "form1",
InsertionMode = InsertionMode.Replace,
OnBegin = "ajaxValidate",
OnSuccess = "getGbPostSuccess",
OnFailure = "showFaliure"
}))
{
采纳答案by Daniel Schaffer
I've been using malsup's jQuery form pluginfor a while for this purpose. I'm honestly not familiar with AjaxHelper, but does look like it'll do what you're looking for. As far as doing both client and server side validation, that should all happen mostly automatically as long as you're using model binding and the attributes from the System.DataAnnotations namespace.
为此,我一直在使用malsup 的jQuery 表单插件。老实说,我对 AjaxHelper 并不熟悉,但看起来它确实可以满足您的需求。至于同时进行客户端和服务器端验证,只要您使用模型绑定和 System.DataAnnotations 命名空间中的属性,这一切都应该自动发生。
public class MyModel
{
[Required(ErrorMessage = "Please enter your name")]
public String Name { get; set; }
[Required(ErrorMessage = "Please enter your email")]
public String Email { get; set; }
[Required(ErrorMessage = "Please enter a rating")]
[Range(1, 5, ErrorMessage = "The rating must be between 1 and 5")]
public Int32 Rating { get; set; }
}
[HttpPost]
public ActionResult Index(MyModel myModel)
{
if(ModelState.IsValid)
{
// good to go, put it in the DB or whatever you need to do
}
else
{
return View(model); // return the user back to the page, ModelState errors can be viewed using Html.ValidationSummary() or individual Html.ValidationMessageFor() calls
}
}
If you're doing your own custom server-side validation, you can either create your own custom validation attribute by creating an attribute that implements ValidationAttribute, or just add validation errors by calling ModelState.Errors.Add() (or something around there, I don't have a reference handy)
如果您正在执行自己的自定义服务器端验证,则可以通过创建实现 ValidationAttribute 的属性来创建自己的自定义验证属性,或者仅通过调用 ModelState.Errors.Add()(或其他类似方法)来添加验证错误,我手边没有参考资料)
For client side, MVC will generate clientside validation for you based on the DataAnnotations attributes on your model.
对于客户端,MVC 将根据模型上的 DataAnnotations 属性为您生成客户端验证。

