MVC 5 built in validation message not showing for decimal types
I'm having an issue where the built in validation message does not show on fields that are decimal types. Int types work as expected and do so without any data annotation. I have the following model:
public class Expense
{
public vwExpenseHeader ExpenseHeader { get; set; }
public tblExpenseDetail[] ExpenseDetail { get; set; }
}
In the view I have this:
@for (int i = 0; i < 7; i++)
{
<td>@Html.EditorFor(m => m.ExpenseDetail[i].Breakfast, new { htmlAttributes = new { @class = "form-control", @onblur = "setCategoryTotal(this.id);" } })</td>
}
In the controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Expense expense)
{
if (ModelState.IsValid)
{
using (AccountingClient acc = new AccountingClient())
acc.ExpenseHeaderInsertOrUpdate(expense.ExpenseHeader, expense.ExpenseDetail);
return View("Saved");
}
else
return View(expense);
}
The ModelState.IsValid call does work but no message appears on the field that failed validation. For the int field
@for (int i = 0; i < 7; i++)
{
<td>@Html.EditorFor(m => m.ExpenseDetail[i].Mileage, new { htmlAttributes = new { @class = "form-control", @onblur = "setCategoryTotal(this.id);" } })</td>
}
it works as expected. The message "Please enter a number." appears.
I have this partial class
[MetadataType(typeof(tblExpenseDetailMetaData))]
public partial class tblExpenseDetail
{
public string DateOfExpenseString
{
get { return DateOfExpense.ToString("MM/dd/yyyy"); }
}
}
and this metadata class
public class tblExpenseDetailMetaData
{
[DataType(DataType.Currency)]
public decimal Breakfast;
}
In the metadata class I've tried:
[Range(0, double.MaxValue, ErrorMessage = "Please enter valid doubleNumber")]
and
[Range(0, 1000)]
Both the int and decimal are nullable types so I tried setting the decimal as a non-nullable type but same result.
Adding @Html.ValidationMessageFor works and I will likely have to use this approach but wouldn't need this if I could get the built in validation message to show.
Any help or suggestions is appreciated.
My bad. New to MVC and was confusing the html5 number attribute on the input with what I thought was built in MVC validation.
链接地址: http://www.djcxy.com/p/56616.html上一篇: C#到Python转换器
下一篇: MVC 5内置验证消息,不会显示小数类型