0

I have the following textboxfor in my view

   @Html.TextBoxFor(m => m.AdvertModel.Title, new
                       {
                           @class = "form-control",
                           type = "text",
                           placeholder = "Enter a descriptive title about the item for sale",
                           id = "Enter a descriptive title",
                           data_val_required = "The Title field is required.",
                           data_val = "true"
                       })

You can see I have added the data_val_required and data_val attributes to do it, this renders as follows:

<input id="Enter a descriptive title" class="form-control" type="text" value="" placeholder="Enter a descriptive title about the item for sale" name="Title" data-val-required="The Title field is required." data-val-maxlength-max="100" data-val-maxlength="The field Title must be a string or array type with a maximum length of '100'." data-val="true">

When I run the application and this and leave it empty and click submit the ModelState.isValid is always true, when I would expect it to be false, why does it keep saying it true?

Code Ratchet
  • 5,758
  • 18
  • 77
  • 141
  • 2
    In order to make it `ModelState.isValid` you need to apply few attributes to the property of model like `[Required]`. Simply adding attribute to html tag wont work. **Note**: `data_val_required` and `data_val` are for client side unobstrusive validation. – Jenish Rabadiya Mar 02 '15 at 07:25
  • @JenishRabadiya I don't have the required attribute on the title variable inside my model, I wanted to figure out if the above was possible without having the [Required] attribute and being able to use ModelState.isValid – Code Ratchet Mar 02 '15 at 07:33
  • @ScottAtkinson even I have same question if possible. Have a look at that question here: http://stackoverflow.com/questions/26352719/disable-system-compositionmodel-dataannotation-from-generating-db-scema-instead :) – Jenish Rabadiya Mar 02 '15 at 07:36
  • @ScottAtkinson You can follow the comment there in question as alternative to the solution. – Jenish Rabadiya Mar 02 '15 at 07:38

1 Answers1

1

When you submit a form to a POST method, the form values contain key/value pairs consisting of each controls name attribute and value attribute. In your case it would be AdvertModel.Title=The value entered in the textbox. No information regarding other attributes in controls are sent to the server.

The data-* attributes are rendered by the html helpers based on validation attributes applied to the model and are useful only if you have the associated @Html.ValidationMessageFor() and include the relevant script files (jquery, jquery.validate and jquery.validate.unobtrusive).

You will get both server side and client side validation if you include the [Required] attribute on the model property

 [Required(ErrorMessage = "The Title field is required.")]
 public string Title { get; set; }

and in the view

@Html.TextBoxFor(m => m.AdvertModel.Title, new { @class = "form-control", placeholder = "Enter a descriptive title about the item for sale", title = "Enter a descriptive title" })
@Html.ValidationMessageFor(m => m.AdvertModel.Title)

Side notes: You do not need type="text" (this is added by the helper) and I assume id = "Enter a descriptive..." is a typo and that its really title = "Enter a descriptive..."

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116