我正在使用MVC的标准验证,与我的流利nHibernate交叉
[DisplayName("Product Name")]
[Required(ErrorMessage = "Product Name Required")]
public virtual string ProductName { get; set; }
[DataType(DataType.MultilineText)]
public virtual string Description { get; set; }
[DataType(DataType.Currency)]
[Required(ErrorMessage = "Price Required")]
public virtual decimal Price { get; set; }
[Required(ErrorMessage = "Quantity Required")]
[Range(0, 100000, ErrorMessage = "Must be postive number less then 100000")]
public virtual int Quantity { get; set; }
public virtual bool Live { get; set; }
public virtual ICollection<Attribute> Attribute { get; set; }
public virtual ICollection<Images> Images { get; set; }
这就是构成我的“产品”类的原因......由于某种原因,该名称并未作为必填字段进行验证,而是数量和价格等。
查看中有这些
<tr>
<td>
<%= Html.LabelFor(model => model.ProductName)%>
</td>
<td>
<%= Html.TextBoxFor(model => model.ProductName, new { @class = "txt" })%>
<%= Html.ValidationMessageFor(model => model.ProductName)%>
</td>
</tr>
这个位工作正常
<tr>
<td>
<%= Html.LabelFor(model => model.Price) %>
</td>
<td>
<%= Html.TextBoxFor(model => model.Price, String.Format("{0:F}", Model.Price)) %>
<%= Html.ValidationMessageFor(model => model.Price) %>
</td>
</tr>
这是要求的控制器
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddProduct(Product NewProduct)
{
if (ModelState.IsValid)
{
var ProductRepository = GetRepository<Product>();
ProductRepository.Add(NewProduct);
return Redirect("/");
}
return View(NewProduct);
}
答案 0 :(得分:1)
试一试:
[DisplayName("Product Name")]
[Required(ErrorMessage = "Product Name Required", AllowEmptyStrings = false)]
public virtual string ProductName { get; set; }
答案 1 :(得分:0)
我发现这是一个没有设置数据类型的问题
[DataType(DataType.Text)]
我的产品名称字段中缺少了!