Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 将modelstate空验证错误与<;输入类型=";按钮“&燃气轮机;_Asp.net Mvc 3_Jquery_Razor - Fatal编程技术网

Asp.net mvc 3 将modelstate空验证错误与<;输入类型=";按钮“&燃气轮机;

Asp.net mvc 3 将modelstate空验证错误与<;输入类型=";按钮“&燃气轮机;,asp.net-mvc-3,jquery,razor,Asp.net Mvc 3,Jquery,Razor,我在mvc razor中有一个表单,它有两个文本框和一个保存电子邮件的按钮。 当我在浏览器中的onclick事件中使用submit按钮时,通过单击它,它会检查电子邮件文本框是否为空,在我的mvc模型中显示验证错误以填充它,但当我填充文本框并单击send按钮时,它不会转到我的SaveEmailForNewsletter()函数,而只是刷新页面 <input type="submit" id="submitBtn" value="send" onclick="javascript

我在mvc razor中有一个表单,它有两个文本框和一个保存电子邮件的按钮。 当我在浏览器中的onclick事件中使用submit按钮时,通过单击它,它会检查电子邮件文本框是否为空,在我的mvc模型中显示验证错误以填充它,但当我填充文本框并单击send按钮时,它不会转到我的SaveEmailForNewsletter()函数,而只是刷新页面

       <input type="submit" id="submitBtn" value="send" onclick="javascript:SaveEmailForNewsletter();" class="ButtonStyle"/>

$("#myform").submit(
    function SaveEmailForNewsletter(e) {
    e.PreventDefault();
        var newsLetter = { Email: $("#Email").val(),Name:$("#Name").val(), LanguageId: "1" };
        $.ajax({
            url: '/Newsletter/Index',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(newsLetter),
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                if (result.success) {
                    // We have a JSON object in case of success
                    alert('register successfuly!');
                    $('#Email').val("");
                    $("#Name").val("");
                } else {
                    // We have the partial with errors in case of failure
                    // so all we have to do is update the DOM
                    alert('This email registered previosly.');
                    $("#Email").val("");
                    $("#Name").val("");    
                 }
               }
             });
            });
它不起作用。以两种方式显示此错误:

“javascript未定义”或“return SaveEmailForNewsletter()未定义” 定义'

当我在浏览器中使用带有“type=button”和onclick事件的输入时,通过单击“send button”,它不会检查空文本框,也不会显示任何错误。当我看到firebug的控制台时,它只是在没有任何东西的情况下发布

  <input type="button" id="submitBtn" value="send" 
       onclick="SaveEmailForNewsletter();" class="ButtonStyle"/>


    function SaveEmailForNewsletter() {    
    //Code above
       });

函数SaveEmailForNewsletter(){
//上面的代码
});
我想选中文本框,如果它们为空,则显示modelstate验证错误,然后转到我的函数,执行并显示警报。 我该怎么办?
谢谢。

您的刷新问题与您的
有关。您正在使用
并提交它,然后覆盖实际提交,除非我认为您当前没有这样做。更好的选择是使用常规按钮

另外,尽量保持JavaScript文件中事件的连接,没有它,标记会更干净

参见上的示例

编辑2:

尝试将函数从$(“#myform”).submit()函数中拉出,使其位于“全局命名空间”中


谢谢,但它不适用于我,当我使用“type=button”时,它不会显示和保存任何内容。当我使用“type=submit”时,它会在我的文本框旁边显示modelstate验证错误,但它不会保存任何内容,也不会显示警报。添加了如何防止提交,也请尝试此操作。谢谢,但我收到了此错误:“ReferenceError:saveEmailForNewsletter未定义[Break On this error]$(“#submitBtn”)。单击(saveEmailForNewsletter);“啊,我想你的问题可能是你的函数在你的提交函数中,所以当你连接它时它就看不见了。我的函数在这个js文件中:'',我使用'$(document).ready(函数(){$('#submitBtn')。单击(saveEmailForNewsletter);});'在它之前或之后。我还将您的代码嵌入到我的service.js文件中。但我仍然得到了这个错误:“ReferenceError:saveEmailForNewsletter未定义”
  <input type="button" id="submitBtn" value="send" 
       onclick="SaveEmailForNewsletter();" class="ButtonStyle"/>


    function SaveEmailForNewsletter() {    
    //Code above
       });
$(document).ready(function () {
    $('#submitBtn').click(saveEmailForNewsletter);
});

function saveEmailForNewsletter() {
    alert('I\'m here!');
}
$("#submitBtn").click(SaveEmailForNewsletter);

function SaveEmailForNewsletter(e) {

    // Use lowercase on preventDefault()
    e.preventDefault();

    var newsLetter = {
        Email: $("#Email").val(),
        Name: $("#Name").val(),
        LanguageId: "1"
    };
    $.ajax({
        url: '/Newsletter/Index',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(newsLetter),
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            if (result.success) {
                // We have a JSON object in case of success
                alert('register successfuly!');
                $('#Email').val("");
                $("#Name").val("");
            } else {
                // We have the partial with errors in case of failure
                // so all we have to do is update the DOM
                alert('This email registered previosly.');
                $("#Email").val("");
                $("#Name").val("");
            }
        }
    });

    // Will stop the submit
    return false;
}