我第一次尝试使用远程验证,但是在传递到远程验证方法的参数时遇到了问题。
我的模型如下:
public class PerinatalWomanView : IPerinatalWoman
{
[Required(ErrorMessage = "The woman's postcode is required")]
[Display(Name = "Woman's postcode")]
[Remote("PostcodeCheck", "Validation", "Validation")]
[RegularExpression("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} [0-9][A-Za-z]{2}$", ErrorMessage = "A valid postcode is required")]
public string Postcode { get; set; }
视图片段是:
<div class="editor-label control-label">
@Html.LabelFor(m => m.perinatalWomanView.Postcode)
</div>
div class="editor-field controls">
@Html.EditorFor(m => m.perinatalWomanView.Postcode)
@Html.ValidationMessageFor(m => m.perinatalWomanView.Postcode)
</div>
验证控制器是:
public class ValidationController : Controller
{
//
// GET: /Validation/Validation/
[HttpGet]
public JsonResult PostcodeCheck(string Postcode)
{
// postcode has already been checked for correct format
// now look it up to see if it exists
string mbrraceCommonCodeEntitiesConnectionString = ConfigurationManager.ConnectionStrings["MbrraceCommonCodeEntities"].ConnectionString;
if (PostcodeChecks.CheckPostcodeExists(mbrraceCommonCodeEntitiesConnectionString, Postcode))
{ return Json(true, JsonRequestBehavior.AllowGet); }
return Json("This postcode was not found in the database", JsonRequestBehavior.AllowGet);
}
当控制器被调用时, Postcode 的值为空。来自 Firebug 的实际调用是:
http://localhost:57881/Validation/Validation /PostcodeCheck?perinatalWomanView.Postcode=OX4+1SU
我如何正确读取邮政编码字符串参数,因为它显然不为空?
帮助!