-2

如果我的服务器端验证通过,我想显示一条消息,我从服务器传回客户端。错误消息是可能的(定义为ASP.Net MVC中属性的属性),我可以显示成功消息吗?也许我可以利用JQuery验证插件来做到这一点?从服务器端验证显示ASP.Net MVC中的成功验证消息

ex。我从该位置进行查找以获取想要将城市作为字符串传回城市的城市,以将其显示为验证成功消息。

这是我的代码。

[Required()] 
    [MaxLength(5)] 
    [MinLength(5, ErrorMessage = "Can't find location")] 
    [Display(Name = "Zip code")] 
    [Remote("CheckLocation", "Account", AreaReference.UseCurrent, ErrorMessage = "Invalid zipcode")] 
    public string Location { get; set; } 


    [HttpGet] 
    [AllowAnonymous] 
    public JsonResult CheckLocation(int Location) 
    { 
     var city = getLocation(Location); 
     if (city != null) 
     { 
      // pass the city back with the JSon object, but how do I display it??? 
      return Json(true, JsonRequestBehavior.AllowGet); 
     } 
     return Json(false, JsonRequestBehavior.AllowGet); 
    } 

编辑这里是我试过了。我在我的c#属性上使用jQuery现在验证而不是远程属性。这工作正常,但我如何在远程定义一个成功属性,并将该成功消息传递给验证消息?然后将颜色更改为绿色而不是红色?

$('#Location').rules("add", { 
    required: true, 
    minlength: 5, 
    maxlength: 5, 
    messages: { 
    required: "Required input", 
    minlength: jQuery.validator.format("Please, {0} characters are necessary"), 
    maxlength: jQuery.validator.format("Please, {0} characters are necessary") 
    }, 
    remote: { 
    url: "CheckLocation", 
    type: "post", 
    data: { 
     Location: function() { 
     return $('#Location').val(); 
     } 
    } 
    } 
}); 

EDIT 2 我真的想显示来自服务器(例如,shoing进入一个zip当城市)一个更迭的消息,所以我觉得我可以在使用一个成功回调远程调用并在回调中操作消息颜色并使元素有效。我认为如果JSon响应中存在字符串,那么validate会将其设置为false?

这样的事情。

 [HttpGet] 
    [AllowAnonymous] 
    public JsonResult CheckLocation(int Location) 
    { 
     var city = getLocation(Location); 
     if (city != null) 
     { 
      return Json(city); // this will be text like "London" 
     } 
     return Json(false); 
    } 
$('#Location').rules("add", { 
    required: true, 
    //minlength: 5, 
    maxlength: 5, 
    messages: { 
    required: "Required input", 
    minlength: jQuery.validator.format("Please, {0} characters are necessary"), 
    maxlength: jQuery.validator.format("Please, {0} characters are necessary") 
    }, 
    remote: { 
    url: "CheckLocation", 
    type: "post", 
    data: { 
     Location: function() { 
     return $('#Location').val(); 
     } 
    }, 
    success: function(data) { 
     // change the color from red to green for the message 
     // make the invalid element valid so it passes submition 
    } 
    } 
}); 
+0

检查这一项就应该帮助http://stackoverflow.com/问题/ 28366314/mvc5-data-annotations-client-side-validation-not-occurrence – mike123

+0

@ user1186050为什么你不能使用错误或成功消息的标签,给它一个id,然后在jquery中设置值取决于返回值? –

+0

我想我可以,但不是有一些JQuery验证引擎盖下的东西吗?我想显示所有验证错误消息。我想确保表单中有无效的字段,然后我尝试提交 – user1186050

回答

1

.validate()方法的​​用于当验证通过控制所述消息元素。默认情况下,错误消息在验证通过时隐藏。当使用success时,您可以修改此行为并在验证通过时显示图标,消息等。但是,此选项不会从您的服务器检索邮件。

jQuery Validate是客户端代码,并且只有一个内置方法会从服务器接收响应,这就是remote方法。但是,这里的问题是,您只能通过true才能通过验证,false验证失败......并且如果您传递字符串,则会将其转换为错误消息和验证失败

AFAIK,没有内置的方法来做你想做的事。也许你可以编写一个自定义success函数,它使用.ajax()从服务器检索消息。


摘要

  1. 插件通常不显示成功的消息。 success选项是强制显示消息的一种方式。但是,问题在于您无法将值传递给此选项。这只是一种在字段有效时控制错误消息对象的方法。

  2. 除了remote方法以外,插件通常不会与服务器通信。这个问题是,你只能通过true通过验证。如果传递一个字符串,验证将失败(,字符串变成验证错误消息)。


编辑回应OP:

但我怎么在这里远程定义一个成功的财产和传递成功消息确认讯息话题?

您只能使用the success option正确地处理错误元素。不过,我认为你不能达到上述目标,因为似乎没有办法将该字段的值传递给success选项。否则,您可以在success之内使用.ajax()来检索您的消息。

然后改变颜色为绿色而不是红色?

这是通过为validerror类定义CSS属性自动完成的。您还可以在success选项中使用.addClass()应用自定义类。

IMO,你的整个目标不能使用jQuery Validate插件来实现。但是,您应该彻底查看the documentation以确保自己。


我想验证它设置为false,如果没有在JSON响应一个字符串?

是的,就像我在回答中所述。如果服务器响应是以外的其他任何内容true,则验证设置为失败。

+0

将无法​​验证远程调用采取成功的属性,我可以用来传递消息回到客户端?那么我不能只在验证信息中显示它,并将它变成绿色或其他什么东西? – user1186050

+0

@ user1186050你说你想显示城市 –

+1

@ user1186050,如果'remote'调用得到*除“true”以外的任何东西*,则该字段被标记为“无效”。 – Sparky

0

你可以做的就是这个

使用Ajax调用自定义的操作方法,而不是

var url = "/MyController/CheckLocation/"; 

$.ajax({ 
    url: url, 
    data: { zipcode: _zipcode }, 
    cache: false, 
    type: "POST", 
    dataType: "json" 
}).done(function (obgcity) { 

    //If objcity is null then display error else display city 
    } 
}); 
+0

这将如何整合到jQuery Validate插件中? – Sparky

+0

@Sparky他可以在他的视图文件中使用它或者将它添加到脚本中,然后为这个脚本添加一个包到他的视图文件 –

+0

@Sparky如果你的意思是对于jQuery脚本文件,那么他可以将这个代码添加到脚本文件 –

0

我希望下面的代码可以帮助你远程数据注解。

public ActionResult CheckLocation(int Location) 
    { 
     var city = getLocation(Location); 
     if (city != null) 
     { 
      // city is string type, example: string city="Mumbai"; 
      return Json(new {Status=true, City = city}, JsonRequestBehavior.AllowGet); 
     } 
     return Json(new { Status=false, City=""}, JsonRequestBehavior.AllowGet); 
    } 

现在,作为Sparky在消息上面提到可为呼叫到你的动作或方法,使用Ajax和编写自定义函数成功

var locationId = 20; 
$.ajax({ 
    url: "MyController/CheckLocation", 
    data: { Location: locationId}, 
    cache: false, 
    type: "GET", 
    dataType: "json", 
    success: function (data) { 
      if(data.Status == true) 
      { 
       alert(data.City); 
       // instead of alert you can create custom message alert box, 
       // or if you want to display city name or whatever message 
       // you can try $("#cityId").Html("<p>this is city</p>"); 
      }   
    }, 
    error: function (xhr, ajaxOptions, thrownError) { 
     alert("Ajax Failed!!!"); 
    } 
});