asp.net MVC 3 - AJAX表单提交和确认 [英] asp.net mvc 3 - ajax form submit and validation

查看:103
本文介绍了asp.net MVC 3 - AJAX表单提交和确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,如果这已经被问了,但我一直在寻找了一段时间,但所有我发现是相当老的帖子(MVC1,MVC2)。
我有,我想通过Ajax提交表单。

<一个href=\"http://stackoverflow.com/questions/298691/asp-net-mvc-ajax-form-with-jquery-validation\">This看起来像它会工作,但不包括服务器端验证。

1)我不确定我是否应该使用 AjaxHelper.BeginForm 或使用原始的jQuery呼叫($。阿贾克斯)?什么是这里建议的方法?

2)如何处理客户端和服务器端验证?我希望MVC框架提供了内置的机制来处理呢?
有一些验证,我现在只是在做服务器端。会使用的ValidationSummary 还在这里工作?

我使用asp.net MVC3 /剃刀unobtrussive JavaScript验证。

感谢您!

编辑:(由下面鲍比b以请求)
这是问这个问题之后添加个月作为一个用户想知道如何使用AjaxHelper

这是JavaScript code我用:

 &LT;脚本类型=文/ JavaScript的&GT;功能ajaxValidate(){
  返回$('形式')验证()的形式()。;
}功能getGbPostSuccess(ajaxContext){
  // ....这是没有必要在这里做什么。
}
功能showFaliure(ajaxContext){
   //处理失败
}

HTML片段:


    

  @using(Ajax.BeginForm(指数,家,新AjaxOptions
                        {
                            UpdateTargetId =Form1的,
                            InsertionMode = InsertionMode.Replace,
                            OnBegin =ajaxValidate
                            的onSuccess =getGbPostSuccess
                            onFailure处=showFaliure
                        }))
{


解决方案

我一直在使用 malsup的jQuery的形式插件一个而用于此目的。我诚实地不熟悉AjaxHelper,它不过看起来像它会做你要找的内容。至于做客户端和服务器端验证,这应该大多都自动只要你使用模型绑定,并从System.DataAnnotations命名空间中的属性发生了。

 公共类为MyModel
{
    [必需(的ErrorMessage =请输入您的姓名)]
    公共字符串名称{;组; }    [必需(的ErrorMessage =请输入您的电子邮件)]
    公共字符串电子邮件{搞定;组; }    [必需(的ErrorMessage =请输入一个等级)
    [范围(1,5,的ErrorMessage =该评级必须在1到5之间)]
    公众的Int32评级{搞定;组; }
}[HttpPost]
公众的ActionResult指数(为MyModel基于myModel)
{
   如果(ModelState.IsValid)
   {
       //好走,把它放在DB或任何你需要做的
   }
   其他
   {
       返回查看(模型); //返回用户回到页,的ModelState错误可以使用Html.ValidationSummary()或个别Html.ValidationMessageFor()调用被看作
   }
}

如果你做你自己的自定义服务器端验证,你可以通过创建实现ValidationAttribute属性创建自己的自定义验证属​​性,或者只是通过调用ModelState.Errors.Add()(或添加一些验证错误身边有,我没有方便的引用)

有关客户端,MVC会为你的基础上的DataAnnotations你的模型属性生成客户方的验证。

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.

This looks like it would work but does not cover server side validation.

1) I am unsure if I should use the AjaxHelper.BeginForm or use raw jquery calls ($.ajax)? What is the recommended approach here?

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 ValidationSummary still work here?

I am using asp.net mvc3/razor with unobtrussive javascript validation.

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

This is the javascript code I used:

<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:

@using (Ajax.BeginForm("Index", "Home", new AjaxOptions
                        {
                            UpdateTargetId = "form1",
                            InsertionMode = InsertionMode.Replace,
                            OnBegin = "ajaxValidate",
                            OnSuccess = "getGbPostSuccess",
                            OnFailure = "showFaliure"
                        }))
{

解决方案

I've been using malsup's jQuery form plugin for 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.

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)

For client side, MVC will generate clientside validation for you based on the DataAnnotations attributes on your model.

这篇关于asp.net MVC 3 - AJAX表单提交和确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆