Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required value in Html.TextBoxFor

I need to put the value of a TextBox required like this :

<td>@Html.TextBoxFor(m =>(m.Code), new { @required  = "required"})</td>

It works. But if i set a default value to the TextBox

<td>@Html.TextBoxFor(m =>(m.Code), new { @Value = @Model.Code, @required  = "required"})</td>

An empty value becomes accepted despite the generation of this Html code

<td><input id="Code" name="Code" required="required" type="text" value="fff       "></td>
  1. What is the source of this problem?
  2. How can i fix it?
like image 433
Lamloumi Afif Avatar asked Jan 17 '14 06:01

Lamloumi Afif


People also ask

What is the difference between HTML TextBox HTML TextBoxFor and HTML EditorFor?

Show activity on this post. TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control. EditorFor: This control is bit smart.

How do you make a TextBoxFor ReadOnly?

The TextBoxes can be made ReadOnly by setting the HTML ReadOnly attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions.

How disable HTML TextBoxFor in MVC?

You can make the input 'read only' by using 'readonly'. This will let the data be POSTED back, but the user cannot edit the information in the traditional fashion. Keep in mind that people can use a tool like Firebug or Dragonfly to edit the data and post it back.

What is TextBoxFor in MVC?

TextBoxFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object) Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.


1 Answers

I don't know why but when i delete the space between = and @

<td>@Html.TextBoxFor(m =>(m.Code), new { @Value [email protected], @required  = "required"})</td>

it works

like image 69
Lamloumi Afif Avatar answered Sep 18 '22 05:09

Lamloumi Afif