我在我的网络应用程序中使用MVC的远程验证。当我尝试使用Remote[]
验证属性
[Remote("EmailExists", "Validation")]
public string EmailAddress { get; set; }
我希望此电子邮件地址与我的ValidationController
联系,并调用方法EmailExists
。
public class ValidationController : Controller
{
public JsonResult EmailExists(string emailAddress)
{
return Membership.GetUser(emailAddress) != null ?
Json("true", JsonRequestBehavior.AllowGet) :
Json("Email does not exist", JsonRequestBehavior.AllowGet);
}
}
渲染的HTML看起来像这样
<input class="form-control" data-val="true" data-val-remote="'EmailAddress' is invalid." data-val-remote-additionalfields="*.EmailAddress" data-val-remote-url="/IdSrv/Validation/EmailExists" id="EmailAddress" name="EmailAddress" type="text" value="">
但相反,如果你看一下data-val-remote-url
,你会注意到当HTML实际渲染时它会呈现完全不同的东西
<input class="form-control" data-val="true" data-val-remote="'EmailAddress' is invalid." data-val-remote-additionalfields="*.EmailAddress" data-val-remote-url="/IdSrv/issue/wstrust?controller=Validation&action=EmailExists" id="EmailAddress" name="EmailAddress" type="text" value="">
我正在使用ThinkTecutre身份提供商,但我不确定为什么Remote
验证设置的默认路由不正确。我做错了什么,或者我可以改变什么来将URL指向正确的位置?
答案 0 :(得分:0)
确保在默认路由之前没有在RoutingConfig.cs
中定义任何自定义路由。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
尝试将属性名称与操作方法参数匹配
public JsonResult EmailExists(string EmailAddress)
{
}
查看以下链接了解更多详情。
http://msdn.microsoft.com/en-us/library/gg508808%28vs.98%29.aspx
http://www.codeproject.com/Tips/669824/Implementing-Remote-Validation-in-MVC
http://www.codeproject.com/Articles/674288/Remote-Validation-in-MVC-Simple-Way-to-Pass-the-F