我正在尝试创建自定义远程数据注释以检查唯一值。
到目前为止,我有:
[Remote("checkForUniqueSpeciesName", "Create", ErrorMessage = "A Species by that name already exists.")]
public string SpeciesName { get; set; }
和
public ActionResult checkForUniqueSpeciesName(string species_name)
{
bool is_unique = ........
return Json(is_unique, JsonRequestBehavior.AllowGet);
}
说实话,我真的不明白这是如何工作的,我只是想跟随网上的例子。我想在提交表单时会调用checkForUniqueSpeciesName
,并且该方法返回true或false。我是否需要在视图中添加验证消息,例如?
@Html.ValidationMessageFor(model => model.SpeciesName, "", new { @class = "text-danger" })
我需要吗?
Model Species.cs: 公共类物种 { [键] public int SpeciesId {get;组; }
[Display(Name = "Species")]
[Required(ErrorMessage = "You must enter a species name.")]
[Remote("CheckForUniqueSpeciesName", "Create", ErrorMessage = "A Species by that name already exists.")]
public string SpeciesName { get; set; }
}
Controller SpeciesController.cs:
namespace Gators3.Controllers { public class SpeciesController:Controller { private GatorsContext db = new GatorsContext();
// GET: Species
public ActionResult Index()
{
return View(db.Species.ToList());
}
// GET: Species/Create
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "SpeciesId,SpeciesName")] Species species)
{
if (ModelState.IsValid)
{
db.Species.Add(species);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(species);
}
public ActionResult CheckForUniqueSpeciesName(string speciesName)
{
using (GatorsContext ctx = new GatorsContext())
{
bool isUnique = !ctx.Species.Any(s => s.SpeciesName == speciesName);
return Json(isUnique, JsonRequestBehavior.AllowGet);
}
}
.
.
.
.
查看视图 - >物种 - > Create.cshtml:
@model Gators3.Models.Species
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Species</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.SpeciesName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SpeciesName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SpeciesName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:2)
我猜表单是在调用checkForUniqueSpeciesName时调用的 提交,方法返回true或false。
不,事实并非如此。 [RemoteAttribute]
会自动为您的页面添加一些JavaScript,它会调用Controller上的方法进行服务器端验证并在页面上显示结果,而无需用户提交整个HTML表单。即当您从文本框中跳出选项时调用验证,而不是在您单击提交时调用。
使用您的代码,我假设您的控制器名为CreateController
?
我猜你是否只是缺少数据访问代码来实际检查唯一性?
所以需要这样的东西:
public ActionResult CheckForUniqueSpeciesName(string speciesName)
{
using (YourEntityFrameworkDbContext ctx = new YourEntityFrameworkDbContext())
{
bool isUnique = !ctx.Species.Any(s => s.SpeciesName == speciesName);
return Json(isUnique , JsonRequestBehavior.AllowGet);
}
}
然后在你看来,你只需要这样的东西:
@Html.ValidationMessageFor(x => x.SpeciesName)
这将显示您在[Remote]
属性中指定的验证邮件。
顺便说一句,正如旁注 - 您已经应用于某些代码的编码约定/套管不会受到大多数C#程序员的欢迎(除非您的团队遵守不寻常的标准)请注意我已应用的格式。
更新 - 我认为您的代码需要具备以下条件:
[Remote("CheckForUniqueSpeciesName", "Species", ErrorMessage="A Species by that name already exists.")]