1

I have a DropDownListFor for which i have validation message as "field required"

@Html.DropDownListFor(m => m.CategoryId, new SelectList(Model.Categories, "CategoryId", "CategoryName"), "-- Select Category--", new { id = "idCategory", style = "float:left;" })
@Html.ValidationMessageFor(model => model.CategoryId) 

but i'm always getting errormessage as "The field CategoryId must be a number"

my model

[Required(ErrorMessage = "This field is required")]
public long CategoryId { get; set; }
Joseph
  • 829
  • 4
  • 15
  • 27

1 Answers1

4

Make sure that the CategoryId property on your view model is a nullable integer:

[Required(ErrorMessage = "field required")]
public int? CategoryId { get; set; }

Also you seem to be binding your DropDownList values to a Categories property on your view model. Ensure that this property is an IEnumerable<T> where T is a type containing CategoryId and CategoryName properties. For example:

public class CategoryViewModel
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
}

and now your view model could look like this:

public class MyViewModel
{
    [Required(ErrorMessage = "field required")]
    public int? CategoryId { get; set; }

    public IList<CategoryViewModel> Categories { get; set; }
}

And most importantly inspect the generated HTML and ensure that the values of all the <option> field of this dropdown are indeed integers:

<select class="input-validation-error" data-val="true" data-val-number="The field CategoryId must be a number." data-val-required="The CategoryId field is required." id="idCategory" name="CategoryId" style="float:left;">
    <option value="">-- Select Category--</option>
    <option value="1">category 1</option>
    <option value="2">category 2</option>
    <option value="3">category 3</option>
    <option value="4">category 4</option>
    <option value="5">category 5</option>
</select>

Notice the data-val-number="The field CategoryId must be a number." attribute that gets added to the <select> element. If the options values are not integers you will get this error.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Wow !! this ans is great ! i am checking my whole project whether i have done it this way or not ! one sec ! And i'm not using options . i'm using categoryid to find corresponding category name(from db) and place it in dropdownlist ! thanks a lot ! :) – Joseph Dec 22 '13 at 10:49
  • my option values are string(category names) – Joseph Dec 22 '13 at 10:51
  • Is there a way to disable data-val-number ???? – Joseph Dec 22 '13 at 10:55
  • Why do you want to disable this message? Your view model property is of integer type, so it makes perfect sense to have it. If on the other hand you want to customize this error message you could write a custom model validator provider: http://stackoverflow.com/q/4828297/29407 – Darin Dimitrov Dec 22 '13 at 11:01
  • I'm using json to get value into dropdownlist $.getJSON('@Url.Action("GetCategory")', { departmentId: DepartmentId }, function (items) { var categorySelect = $('#idCategory'); categorySelect.empty(); categorySelect.append( $('') .attr('value',null) .text("-- Select Category--")); i was passing all the time value as null to server !! how could i change it so that i get my errormessage correct ?? – Joseph Dec 23 '13 at 03:47
  • Is there a way I could pass the value as zero and get the errormessage ?? i tried it but but got error as there is no category with id as 0 !!!! – Joseph Dec 23 '13 at 03:56
  • DID IT !! categorySelect.append(""); Thanks Darin ! Couldnt have done it without you ! :) – Joseph Dec 23 '13 at 04:42