【发布时间】:2015-06-08 22:12:48
【问题描述】:
我正在尝试创建自定义远程数据注释来检查唯一值。
到目前为止我有:
[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: 公共类物种 { [钥匙] 公共 int SpeciesId { 获取;放; }
[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:
命名空间 Gators3.Controllers { 公共类 SpeciesController : 控制器 { 私有 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")
}
【问题讨论】:
-
我认为你最好不要再跟着你不理解的例子,而是学会理解它们。
-
是的,您需要
@Html.ValidationMessageFor()才能显示消息(但它必须是m => m.species_name- 即匹配属性名称和控制器参数名称 - 或者如果属性是SpeciesId,那么方法参数需要是SpeciesId)。并且在提交表单时不会调用您的方法 - 它是预先调用的,如果该方法返回false,则在更正错误之前不会提交表单 -
@StephenMuecke -- 是的,我的意思是
SpeciesName,我做了剪切和粘贴。感谢您清除验证运行的时间。关于在“...”中添加什么有什么想法吗? -
没有看到更多代码,无法判断,但可能类似于
bool is_unique = !db.Species.Any(s => s.SpeciesName == SpeciesName)
标签: asp.net asp.net-mvc custom-data-attribute