Remote Validation in ASPNET 5 MVC 6

大城市里の小女人 提交于 2019-12-07 09:34:23

问题


Can not find JsonRequestBehavior in aspnet 5

I am trying to implement remote validation demo and it seem like Microsoft.AspNet.Mvc does not contain JsonRequestBehavior enumeration. But it does exist in System.Web.Mvc namespace in previous version of MVC

Model:

public class Person : Entity
    {
        [Required]
        [StringLength(512)]
        [Remote("IsAllowedName", 
                "Validation", 
                ErrorMessage="This name is not allowed!"
               )]
        [Display(Name = "First (and middle) name")]
        public String FirstMidName { get; set; }

View:

...
<input asp-for="FirstMidName"/>
<span asp-validation-for="FirstMidName"></span>
...

Controller:

[HttpGet]
public JsonResult IsAllowedName(string FirstMidName)
{
    if (FirstMidName.ToLower() == "oleg")
    {
        return Json(false, JsonRequestBehavior.AllowGet); 
    }
    return Json(true);
}

Terminal output:

MacBook-Air-Anton:labefmvc antonprudkoglyad$ dnu build 
...
/Users/antonprudkoglyad/Projects/LabEFMVC/LabEFMVC/Controllers/
ValidationController.cs(20,24):
DNXCore,Version=v5.0 error CS0103: The name 'JsonRequestBehavior'
does not exist in the current context

Build failed.

回答1:


In ASP.NET Core RC1, [Remote] attribute is in the Microsoft.AspNet.Mvc namespace. In ASP.NET Core RC2, [Remote] attribute is in the Microsoft.AspNetCore.Mvc namespace, I believe.

using Microsoft.AspNet.Mvc;

[Remote("IsAllowedName", "Validation", ErrorMessage="This name is not allowed!" )]


来源:https://stackoverflow.com/questions/33321996/remote-validation-in-aspnet-5-mvc-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!