asp.net-mvc - 远程验证 Json MVC
问题描述
在提交表单之前,我需要验证用户的输入字段值。我在自定义控制器中创建了一个动作,并用 Month 装饰了该字段。
[Required]
[DataType(DataType.Date)]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:Y}")]
[DisplayFormat(DataFormatString = "{0:dd/MMM/yyyy}")]
[Display(Name = "Month")]
[Remote("IsvalidMonth", "Objectives", AdditionalFields = "UserID,SalesObjectiveID", ErrorMessage = "Month already existing")]
//[DisplayFormat(DataFormatString = "{0:MMM-yyyy}")]
public Nullable<System.DateTime> Month { get; set; }
我的验证是:
public JsonResult IsvalidMonth(DateTime Month, int? SalesObjectiveID, int? UserID)
{
var monthexist = _context.TBL_SalesObjective.Where(x => x.Month == Month && x.SalesObjectiveID == SalesObjectiveID && x.UserID == UserID).FirstOrDefault().Month;
return Json(!monthexist, JsonRequestBehavior.AllowGet);
}
但我不能使用 !monthexist 因为运营商!不能应用。
如何在 Monht 中进行验证?
解决方案
这 '!' 运算符可以与布尔值一起使用,因为你正在做.FirstOrDefault().Month
的不是布尔值,所以你不能使用'!' 操作员。
IsvalidMonth()
请在您的方法中尝试以下代码
var monthexist = _context.TBL_SalesObjective.Any(x => x.Month == Month && x.SalesObjectiveID == SalesObjectiveID && x.UserID == UserID);
推荐阅读
- sql - 更新减少到 14 个字符的 SQL 行
- java - SceneBuilder中的JavaFX组件位置问题
- python - Python Pandas Groupby -- by = list 给我一个错误?
- c# - C# 8 中的范围和索引类型是什么?
- excel - 根据标准生成随机名称列表
- sql - 如何在没有聚合的情况下分组?
- java - 在Java中将两个“整数”值与“==”进行比较是否安全?
- django - ModuleNotFoundError:使用 apache 进行 django 部署时没有名为“new”的模块错误
- django - 如何路由我的 API 调用以收集具有连接到特定模型实例的 ForeignKey 的所有对象?
- postgresql - 仅针对某些客户端的 Postgres 连接失败。消息:`GSSAPI 身份验证不支持`