0

在进行 AJAX 调用方面,我们使用了以下方法:

    $.ajax({
             type: "POST",
             async: false,
             url: '@Url.Action("CheckPhone", "Progg")',
             data: { input: WebPhoneNum
             },
             success: function (iReturn) {
                 if (iReturn == 0) {
                     alert(Phone Number must be in format (XXX) XXX-XXXX. Please Re-Enter');
                     submitval = false;
                 }
             },
             error: function (xhr, status, error) {
                 var err = eval("(" + xhr.responseText + ")"); // Boil the ASP.NET AJAX error down to JSON.                    
                 alert('Call to CheckPhone failed with error:  ' + err.Message); // display the error raised by the server
             }
         });

注意它如何调用控制器并返回一个值。我想知道是否有更好的方法来做到这一点。我知道 .NET MVC 内置了一些 ajax 调用,但找不到适合我在下面所做的。我知道 .NET MVC 有一个带有超链接的 ajax 调用构建,但这不是我需要的。我只需要调用返回一些值的控制器。

谢谢

4

2 回答 2

0

您只需要解决一件事:

async: false

必须变成:

async: true

因为否则你不是在做 AJAX。您正在向服务器发送同步请求,冻结客户端浏览器。

Other than that you seem to be doing some remote validation with this AJAX request. You probably might take a look at the [Remote] attribute that's built in and which avoids you writing all this code. All you need to do is decorate the view model property that needs to be validated with this attribute and then enable unobtrusive client side validation by including the proper scripts.

于 2012-05-29T15:19:58.763 回答
0

try to validate the format of a input with ajax? I recommend you learn about data annotations to validate that kind of details (please revalidate on server at submit form like a good practice)

here is a usefull tutorial http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-annotation-validators-cs

于 2012-05-29T16:18:39.690 回答