无法通过远程验证提交
Can not submit with Remote Validation
我的远程验证工作正常,但是当我提交表单时,光标聚焦在有效字段上,没有错误消息。
这是我的代码:
项目模型:
[Required]
[Remote("ProjectNameVerify", "Projects")]
public string Name { get; set; }
项目控制器:
public ActionResult ProjectNameVerify(string name)
{
// ...
return Json("msg", JsonRequestBehavior.AllowGet);
}
Project.cshtml:
@using (Html.BeginForm())
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", Autofocus = "false" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
<br>
</div>
<br>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-success" />
</div>
</div>
</div>
将 ProjectNameVerify
控制器更改为此。为false,则显示错误信息,为true,则可以提交表单。
public JsonResult ProjectNameVerify(string name)
{
// ...
return Json({true/false}, JsonRequestBehavior.AllowGet);
}
我的最佳实践:
项目模型:
[Required]
[Remote("ProjectNameVerify", "Projects")]
public string Name { get; set; }
项目控制器:
public JsonResult ProjectNameVerify(string name)
{
if (Verify() == false)
{
return Json("errormsg", JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
我的远程验证工作正常,但是当我提交表单时,光标聚焦在有效字段上,没有错误消息。
这是我的代码:
项目模型:
[Required]
[Remote("ProjectNameVerify", "Projects")]
public string Name { get; set; }
项目控制器:
public ActionResult ProjectNameVerify(string name)
{
// ...
return Json("msg", JsonRequestBehavior.AllowGet);
}
Project.cshtml:
@using (Html.BeginForm())
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", Autofocus = "false" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
<br>
</div>
<br>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-success" />
</div>
</div>
</div>
将 ProjectNameVerify
控制器更改为此。为false,则显示错误信息,为true,则可以提交表单。
public JsonResult ProjectNameVerify(string name)
{
// ...
return Json({true/false}, JsonRequestBehavior.AllowGet);
}
我的最佳实践:
项目模型:
[Required]
[Remote("ProjectNameVerify", "Projects")]
public string Name { get; set; }
项目控制器:
public JsonResult ProjectNameVerify(string name)
{
if (Verify() == false)
{
return Json("errormsg", JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}