在ASP.NET MVC 3的Ajax / jQuery的回发奇怪的验证问题 [英] Strange validation problems with a Ajax/jQuery postback in ASP.NET MVC 3

查看:138
本文介绍了在ASP.NET MVC 3的Ajax / jQuery的回发奇怪的验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在ASP.NET MVC 3一个Ajax / jQuery的回发的问题。


  • 如果验证的失败的下面的code,预期的结果,并给出了形式不会发布。

  • 如果验证的成功的,下面的功能不提交表单和表单提交击中,并返回一个JSON文件浏览器。

如果任何人都可以在这一些启发,我会AP preciate它。

我包括我的部分视图控制器的下面。

我查看

  @model TheLayer.EF.NoteAddPartial@ {使用(Html.BeginForm(创建,注意,新{面积=},FormMethod.Post,新{ID =noteAdd}))
{@ Html.TextAreaFor(M = GT; m.Note_Text,新{@class =音符输入})//注意输入
@ Html.ValidationMessageFor(型号=> model.Note_Text)<输入类型=提交值=发送/>}}

我的客户端脚本(在视图中)

 <脚本类型=文/ JavaScript的>$(函数(){    $('#noteAdd')。提交(函数(五){        亦即preventDefault();        如果($(本).valid()){
            警报('发布');
            $阿贾克斯({
                网址:this.action,
                类型:this.method,
                数据:$(本).serialize()
                错误:功能(XHR,ajaxOptions,thrownError){
                    警报('发生错误处理这个请求时:\\ r \\ n \\ r \\ n'+ thrownError);
                },
                成功:函数(结果){
                    警报(result.s);
                }
            });
        }
        返回false;
    });
});

我控制器

 公共类NoteController:控制器
{
    的[AcceptVerbs(HttpVerbs.Post)
    公共JsonResult创建(NoteAddPartial模型)
    {
        尝试
        {
            NoteMethods.CreateLeadNote(model.Note_Text,SessionManager.Current.ActiveUser.Username,model.ItemId,SessionManager.Current.ActiveUser.Company);
            返回JSON(新{S =成功});
        }
        赶上(NoPermissionException当)
        {
            返回JSON(新{S =没有权限});
        }    }
}

我的偏

 命名空间XX
{
///<总结>
///部分铅人称视角
///< /总结>
[MetadataType(typeof运算(NoteAddPartial_Validation))]
公共部分类NoteAddPartial
{
    公共字符串Note_Text {搞定;组; }
    公众诠释?来源{搞定;组; }
    公共字符串用户名{获得;组; }
    公众诠释ITEMID {搞定;组; }
    公众诠释TYPEID {搞定;组; }}
公共类NoteAddPartial_Validation
{    [需要]
    公共字符串Note_Text {搞定;组; }    [HiddenInput(DisplayValue = FALSE)]
    公众诠释ITEMID {搞定;组; }    [HiddenInput(DisplayValue = FALSE)]
    公众诠释TYPEID {搞定;组; }}
}


解决方案

你添加客户端验证库?


  

jquery.validate.min.js


  
  

jquery.validate.unobtrusive.min.js


注意:


  

如果您配置您应用程式与不显眼的审定工作,你
  并不需要引用第二文库


I am having problems with an Ajax/jQuery postback in ASP.NET MVC 3.

  • If validation fails on the code below, the expected result is given and the form does not post.
  • If validation succeeds, the function below is not hit upon form submission and the form submits and returns a json file to the browser.

If anybody could shed some light on this, I'd appreciate it.

I've included my partial, view and controller below.

My View

@model TheLayer.EF.NoteAddPartial

@{using (Html.BeginForm("Create", "Note", new { area = "" }, FormMethod.Post, new { id = "noteAdd" }))
{ 

@Html.TextAreaFor(m => m.Note_Text, new { @class = "note-input"  })  //note-input
@Html.ValidationMessageFor(model => model.Note_Text)     

<input type="submit" value="Send" /> 

}}

My client side script (within the view)

<script type="text/javascript">

$(function () {

    $('#noteAdd').submit(function (e) {

        e.preventDefault();

        if ($(this).valid()) {
            alert('posting');
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('An error occured when processing this request:\r\n\r\n' + thrownError);
                },
                success: function (result) {
                    alert(result.s);
                }
            });
        }
        return false;
    });
});

My Controller

public class NoteController : Controller
{
    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Create(NoteAddPartial model)
    {
        try
        {
            NoteMethods.CreateLeadNote(model.Note_Text, SessionManager.Current.ActiveUser.Username, model.ItemId, SessionManager.Current.ActiveUser.Company);
            return Json(new { s = "Success" });
        }
        catch (NoPermissionException)
        {
            return Json(new { s = "No permission" }); 
        }

    }
}

My Partial

namespace XX
{
/// <summary>
/// Partial lead person view
/// </summary>
[MetadataType(typeof(NoteAddPartial_Validation))]
public partial class NoteAddPartial
{
    public string Note_Text { get; set; }
    public int? Source { get; set; }
    public string Username { get; set; }
    public int ItemId { get; set; }
    public int TypeId { get; set; }

}
public class NoteAddPartial_Validation
{

    [Required]
    public string Note_Text { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int ItemId { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int TypeId { get; set; }

}
}

解决方案

did you add client side validation libraries?

jquery.validate.min.js

jquery.validate.unobtrusive.min.js

NOTE :

If you configured you app not to work with unobtrusive validation, you do not need to reference the second library.

这篇关于在ASP.NET MVC 3的Ajax / jQuery的回发奇怪的验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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