Rohan Büchner
Rohan Büchner

Reputation: 5393

Razor property without a html value

My textboxes look something like this at the moment,

 @Html.TextBox("Something", "", new
       {                           
          type = "text",
          min = 1,
          max = 60,
          data_validation_integer_regex = @ValidationPatterns.Integer,
          data_validation_integer_message = @ValidationPatterns.IntegerMessage,
          data_bind = "value: observables.Something"
        })

Which renders as

   <input data-bind="value: observables.Something" 
          data-validation-integer-message="Some message" 
          data-validation-integer-regex="^[1-9]\d*$" 
          id="Something" 
          max="60" 
          min="1" 
          name="Something" 
          type="text" 
          value="">

The validation library we're using jqBootstrapValidation

I'd like to make some of my fields required, and the way to do so using this library, I'd need to add a tag to the input field like so

<input id="Something" ... required >

Razor doesn't allow me to do this though, as it seems to want all parameters in this format

property="something" or property=100

is my only option reverting to standard html markup if i want to make my fields required?

Upvotes: 4

Views: 1233

Answers (1)

McGarnagle
McGarnagle

Reputation: 102753

If you use a property with an empty string (required=""), then Razor will render it as a valueless attribute. That is,

@Html.TextBox("name", "value", new { attr="something", required="" })

Renders as:

<input type='text' name='name' id='id' value='value' attr='something' required />

Upvotes: 4

Related Questions