模型中 2 个不同属性的相同远程验证
Same Remote Validation for 2 different properties in a model
我在一个模型中有 2 个属性 contractor1 和 contractor2,如何对它们使用单个远程验证
[Display(Name ="Contractor 1:")]
[Remote("ValidateContractor", "Contracts")]
public string Cntrctr1 {get; set;}
[Display(Name = "Contractor 2:")]
[Remote("ValidateContractor", "Contracts")]`enter code here`
public string Cntrctr2 {get; set;}
控制器中的远程验证功能
public JsonResult ValidateContractor1(string Cntrctr)
{
var valid = Validations.ValidateContractor(Cntrctr);
if (!valid)
{return Json("Enter correct contractor", JsonRequestBehavior.AllowGet);}
else{return Json(true, JsonRequestBehavior.AllowGet);}
}
public static bool ValidateContractor(string CntrctrNM)
{
bool valid;
using (var entities = new CAATS_Entities())
{
var result = (from t in entities.PS_VENDOR_V
where (t.VNDR_1_NM).Equals(CntrctrNM)
select t).FirstOrDefault();
if (result != null)
{
valid = true;
}
else
{
valid = false;
}
}
return valid;
}
这行不通。你能帮我解决这个问题吗?
调用远程验证时,querystring键是字段的名称,例如在你的情况下 /Contracts/ValidateContractor1?Cntrctr1=foo
。您需要一个更动态的解决方案。
实现此目的的一种方法是在 ValidateContractor1
中不包含任何参数,而是只获取第一个查询字符串值。这未经测试,但应该适合您:
public JsonResult ValidateContractor1()
{
// gets the name of the property being validated, e.g. "Cntrctr1"
string fieldName = Request.QueryString.Keys[0];
// gets the value to validate
string Cntrctr = Request.QueryString[fieldName];
// carry on as before
var valid = Validations.ValidateContractor(Cntrctr);
if (!valid)
{return Json("Enter correct contractor", JsonRequestBehavior.AllowGet);}
else{return Json(true, JsonRequestBehavior.AllowGet);}
}
添加到 Rhumborls 答案中,如果您发现他的方法不起作用,可能是因为您使用的是表格;如果是这种情况,您需要使用 Form 属性而不是 QueryString。
public JsonResult ValidateContractor()
{
// gets the name of the property being validated, e.g. "Cntrctr1"
string fieldName = Request.Form.Keys[0];
// gets the value to validate
string Cntrctr = Request.Form[fieldName];
// carry on as before
var valid = Validations.ValidateContractor(Cntrctr);
if (!valid)
{return Json("Enter correct contractor", JsonRequestBehavior.AllowGet);}
else{return Json(true, JsonRequestBehavior.AllowGet);}
}
我在一个模型中有 2 个属性 contractor1 和 contractor2,如何对它们使用单个远程验证
[Display(Name ="Contractor 1:")]
[Remote("ValidateContractor", "Contracts")]
public string Cntrctr1 {get; set;}
[Display(Name = "Contractor 2:")]
[Remote("ValidateContractor", "Contracts")]`enter code here`
public string Cntrctr2 {get; set;}
控制器中的远程验证功能
public JsonResult ValidateContractor1(string Cntrctr)
{
var valid = Validations.ValidateContractor(Cntrctr);
if (!valid)
{return Json("Enter correct contractor", JsonRequestBehavior.AllowGet);}
else{return Json(true, JsonRequestBehavior.AllowGet);}
}
public static bool ValidateContractor(string CntrctrNM)
{
bool valid;
using (var entities = new CAATS_Entities())
{
var result = (from t in entities.PS_VENDOR_V
where (t.VNDR_1_NM).Equals(CntrctrNM)
select t).FirstOrDefault();
if (result != null)
{
valid = true;
}
else
{
valid = false;
}
}
return valid;
}
这行不通。你能帮我解决这个问题吗?
调用远程验证时,querystring键是字段的名称,例如在你的情况下 /Contracts/ValidateContractor1?Cntrctr1=foo
。您需要一个更动态的解决方案。
实现此目的的一种方法是在 ValidateContractor1
中不包含任何参数,而是只获取第一个查询字符串值。这未经测试,但应该适合您:
public JsonResult ValidateContractor1()
{
// gets the name of the property being validated, e.g. "Cntrctr1"
string fieldName = Request.QueryString.Keys[0];
// gets the value to validate
string Cntrctr = Request.QueryString[fieldName];
// carry on as before
var valid = Validations.ValidateContractor(Cntrctr);
if (!valid)
{return Json("Enter correct contractor", JsonRequestBehavior.AllowGet);}
else{return Json(true, JsonRequestBehavior.AllowGet);}
}
添加到 Rhumborls 答案中,如果您发现他的方法不起作用,可能是因为您使用的是表格;如果是这种情况,您需要使用 Form 属性而不是 QueryString。
public JsonResult ValidateContractor()
{
// gets the name of the property being validated, e.g. "Cntrctr1"
string fieldName = Request.Form.Keys[0];
// gets the value to validate
string Cntrctr = Request.Form[fieldName];
// carry on as before
var valid = Validations.ValidateContractor(Cntrctr);
if (!valid)
{return Json("Enter correct contractor", JsonRequestBehavior.AllowGet);}
else{return Json(true, JsonRequestBehavior.AllowGet);}
}