我正在努力让我的双重验证工作,我真的不知道为什么它不起作用。
我的模型如下:
[Required]
[Remote("CheckForHourRange", "Bookings")]
public double ReservationLength { get; set; }
到目前为止这么好吗? 看着控制器,它是:
public JsonResult CheckForHourRange(double hours)
{
var integerPart = Math.Truncate(hours);
var fractionalPart = hours - Math.Truncate(hours);
double fractionalDigits = fractionalPart.ToString(CultureInfo.InvariantCulture).Length;
if (integerPart <= 14 && fractionalDigits <= 1)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
if (hours <= 0)
{
return Json("The length can't be less or equal to 0", JsonRequestBehavior.AllowGet);
}
if (integerPart > 14)
{
return Json("Sorry, we are open from 10.00 to 00.00, thus the hours can't be more than 14", JsonRequestBehavior.AllowGet);
}
if (integerPart <= 14 && fractionalDigits > 1)
{
return Json("Sorry, the hour must be only one place after comma", JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
一旦我尝试运行代码,我就会收到以下错误:
The parameters dictionary contains a null entry for parameter 'hours' of non-nullable type 'System.Double' for method 'System.Web.Mvc.JsonResult CheckForHourRange(Double)' in 'Web.Controllers.BookingsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
参数名称:参数
我已经调试了它,我知道控制器没有运行验证部分,这很奇怪,因为这个工作。
[Required, Range(1, 30, ErrorMessage = "The table number must be between 1 and 30")]
[Remote("CheckForTableExist", "Tables")]
public int TableNumber { get; set; }
public JsonResult CheckForTableExist(int TableNumber)
{
var data = _uow.Tables.All.FirstOrDefault(a => a.TableNumber.Equals(TableNumber));
if (data != null)
{
return Json("Sorry, this number of table already exists", JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
我确信这与地图路由无关,因为我还没有改变它。
希望得到你的帮助!〜
我同意这是一个编辑,我只是在这里添加控制器,如果其他人有一天需要它
public JsonResult CheckForHourRange(double reservationLength)
{
var integerPart = 0;
var fractionalPart = 0;
string s = reservationLength.ToString();
string[] parts = s.Split('.');
try
{
integerPart = (int) Convert.ToDouble(parts[0]);
fractionalPart = (int) Convert.ToDouble(parts[1]);
}
catch { }
var fractionalDigits = fractionalPart.ToString().Length;
if (!(integerPart >= 0)) return Json("Hours cannot be negative", JsonRequestBehavior.AllowGet);
if (fractionalDigits > 2)
{
return Json("Sorry, there cannot be more than one number after comma", JsonRequestBehavior.AllowGet);
}
if (integerPart == 0)
{
return fractionalDigits > 1 ? Json("Sorry, there cannot be more than one number after comma", JsonRequestBehavior.AllowGet) : Json(true, JsonRequestBehavior.AllowGet);
}
if (!(integerPart >= 14))
return fractionalDigits > 1
? Json("Sorry, there cannot be more than one number after comma", JsonRequestBehavior.AllowGet)
: Json(true, JsonRequestBehavior.AllowGet);
if (integerPart != 14)
return
Json(
"Sorry, we are open from 10:00 to 00:00, thus we are open 14 hours, therefore enter correct value",
JsonRequestBehavior.AllowGet);
return fractionalDigits > 0 ? Json("Sorry, we are open from 10:00 to 00:00, thus we are open 14 hours, therefore enter correct value", JsonRequestBehavior.AllowGet) : Json(true, JsonRequestBehavior.AllowGet);
}