MVC3 Ajax和验证问题 [英] MVC3 Ajax and Validation problem

查看:67
本文介绍了MVC3 Ajax和验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在Ajax验证中遇到一些错误.首先,当文本
时,我按下了提交按钮 框为空.然后错误消息已显示在文本框中为空,但它将同时触发Save方法.尽管出现错误消息,
该值保存在数据库内部.请帮助我.

这是我的代码

Hi All,

I got some error in Ajax validation.first, i press the submit button, when the text
box is empty. Then error message had been shown in the text box that is empty but it will trigger Save methods at the same time. Although error message is appeared,
the value is save inside the database. pls help me.

here is my code

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $("#save").click(function () {
            var par = $("#SchemeName").val();
                $.ajax({
                    type: "POST",
                    url: "./../Scheme/Save",
                    data: "{ctschemehead: '" + par.toString() + "'}",
                    datatype: "Json",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        $("#retresult").val(data);
                    },
                    error: function (data) {
                        alert(data);
                    }
                });
        });
        $("#frmreg").validate({
            rules: {
                SchemeName: { required: true }
            },
            messages: {
                SchemeName: "SchemeName is missing!"
            }
        });
    });

</script>


这是保存功能的Ajax操作


Here is the ajax action for save function

public JsonResult Save(string ctschemehead)
        {
            if (ModelState.IsValid)
            {
                string msg = ctschemehead;
                ctSchemeRepoistory.Create_SchemeHead(msg, User.Identity.Name);
                string data = "successful";
                return Json(data, "application/json; charset=utf-8",JsonRequestBehavior.AllowGet);
            }
            else
            {               
                string data = "fail";
                return Json(data,"application/json; charset=utf-8",JsonRequestBehavior.AllowGet);
            }

        }

推荐答案

(文档).ready(
(document).ready(function () {


( #save").click(功能(){ var par =
("#save").click(function () { var par =


(" ).val();
("#SchemeName").val();


这篇关于MVC3 Ajax和验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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