我正在尝试为仓库库存数据库创建新零件,并且我的验证器无法正常工作。这就是正在发生的事情。
输入:a结果:空白(无) 输入:b结果:空白(无) 输入:abc结果:空白(无) 输入:abcdsfasd结果:空白(无) 输入:123结果:空白(通过) 输入:结果:REQ 输入:12345结果:REG EXP NOT MATCHING 输入:123结果:空白(通过) 输入:结果:REQ 输入:a123bc223结果:REQ
所以,我首先必须在(1-9999)中输入有效条目或通过我的4位数要求以获得REG EXP来捕获它,然后我开始在任何输入的字母上获得REQ错误并且REG EXP NOT MATCHED 5个以上的数字。
为什么REG EXP没有打字?为什么REQ仅在输入有效条目后才会命中?当然,如果我点击提交,REQ错误将会发生但我希望REG EXP在第一次输入“a”时立即命中。
型号:
namespace Inventory.Database
{
[MetadataType(typeof(InventoryPartsMetaData))]
public partial class Table_Part_Warehouse
{
}
public class InventoryPartsMetaData
{
[RegularExpression(@"[1-9][0-9]{0,3}",ErrorMessage = "REG EXP NOT MATCHED")]
[Required(ErrorMessage = "REQ")]
[DisplayName("CQ P/N")]
public int C_CQPN { get; set; }
}
}
控制器:
namespace Inventory.Controllers
{
[Authorize]
public class InventoryController : Controller
{
InventoryPartsContext db = new InventoryPartsContext();
// GET: Parts
public ActionResult Parts()
{
return View(db.Table_Part_Warehouse.ToList());
}
[HttpGet]
public ActionResult PartsCreate()
{
ViewBag.Count = db.Table_Part_Warehouse.Count() + 1;
return View();
}
[HttpPost]
public ActionResult PartsCreate(Table_Part_Warehouse tb_part_warehouse)
{
if (ModelState.IsValid)
{
db.Table_Part_Warehouse.Add(tb_part_warehouse);
db.SaveChanges();
return RedirectToAction("Parts");
}
return View(tb_part_warehouse);
}
}
}
查看:
@model Inventory.Database.Table_Part_Warehouse
@{
ViewBag.Title = "Add New Part";
}
<h2>Add New Part</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.C_CQPN, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.C_CQPN, new { htmlAttributes = new { @class = "form-control", @Value = ViewBag.Count } })
@Html.ValidationMessageFor(model => model.C_CQPN, "", new { @class = "text-danger" })
</div>
</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>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
布局:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("Scripts", required: false)
软件包:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));