Im sitting here scratching my head with a validation problem in ASP MVC3.
Somehow I’m able to validate the field Quantity, but the field OrderNumber does not validate. I can leave it empty and it still accepts it. I’ve tried to add other restrictions to it as well (such as max and min length) but same result – it accepts anything.
I also try changing ‘TextBoxFor’ to ‘EditorFor’ – but it’s the same result.
Quantity on the other hand works as I want it. It requires you to enter an integer and it cannot be blank.
Hopefully some of you will be able to see what I’m doing wrong here 🙂
Here is my model:
public class Order
{
[Required(ErrorMessage="Insert Ordernumber (6-digits)")]
public string OrderNumber { get; set; }
[Required]
public string Partnumber { get; set; }
[Required]
public long Quantity { get; set; }
public Order()
{
}
}
And here is my view :
model POWeb.Models.AddModel
@using (Html.BeginForm(“Add”, “Home”, FormMethod.Post))
{
//Create table
<table>
<tr>
<td>Select Partnumber to produce</td>
<td>@Html.DropDownListFor(model => model.SelectedPartNumber, Model.PartNumbers)</td>
</tr>
<tr>
<td>Enter PO number</td>
<td>@Html.TextBoxFor(model => model.OrderNumber)@Html.ValidationMessageFor(model => model.OrderNumber)</td>
</tr>
<tr>
<td>Quantity</td>
<td>@Html.TextBoxFor(model => model.Quantity)@Html.ValidationMessageFor(model => model.Quantity)</td>
</tr>
<tr>
<td colspan="2">
<button type="submit" name="SubmitButton">Add</button>
</td>
</tr>
</table>
}
You have the view of type
POWeb.Models.AddModel, but you try to validateOrdertype. I’m pretty sure validation attributes on those types are not the same, so you get problems