在ASPNET 5 MVC 6远程验证 [英] Remote Validation in ASPNET 5 MVC 6

查看:279
本文介绍了在ASPNET 5 MVC 6远程验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASPNET找不到JsonRequestBehavior 5



我想实现远程验证演示,它看起来像Microsoft.AspNet.Mvc不包含JsonRequestBehavior枚举。
,而它在System.Web.Mvc命名空间中存在的以前版本的MVC



模型:

 公共类人:实体
{
[必填]
[StringLength(512)]
[远程(IsAllowedName,
验证,
的ErrorMessage =此名称是不允许的!

[显示(名称=第一(和中间)名称)]
公共字符串FirstMidName {搞定;组; }

查看:

  ... 
<输入ASP换=FirstMidName/>
<跨度ASP验证换=FirstMidName>< / SPAN>
...



控制器:

  [HTTPGET] 
公共JsonResult IsAllowedName(字符串FirstMidName)
{
如果(FirstMidName.ToLower()==奥列格)
{
返回JSON(假,JsonRequestBehavior.AllowGet);
}
返回JSON(真);
}



端子输出:

 的MacBook空中安东:labefmvc antonprudkoglyad $ DNU建立

/用户/ antonprudkoglyad /项目/ LabEFMVC / LabEFMVC /控制器/
ValidationController.cs(20,24):
DNXCore,版本= 5.0版错误CS0103:名称'JsonRequestBehavior'
不存在于当前上下文

合同存在失败。


解决方案

在ASP.NET核心RC1,[远程]属性在 Microsoft.AspNet.Mvc 命名空间。
在ASP.NET核心RC2,[远程]属性是在 Microsoft.AspNetCore.Mvc 命名空间,我相信。

 使用Microsoft.AspNet.Mvc; 

[远程(IsAllowedName,验证,的ErrorMessage =此名称是不允许的!)]


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.

解决方案

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!" )]

这篇关于在ASPNET 5 MVC 6远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆