문제

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?
도움이 되었습니까?

해결책

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

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

it works

다른 팁

Use

@Html.ValidationMessageFor(m => m.Code)

for validating the code property.and code Property should be define like this-

 [Required]
 public string Code{ get; set; }

and for setting the value in code Textbox.You can set it in controller like this.

Model.Code="fffff";

and on the view use like this-

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

and yes dont forgot to include the validation Js i.e

 <script src="/Scripts/jquery.validate.unobtrusive.js"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>

Try it:-

you don't need the parens

@Html.TextBoxFor(m => m.Code, new { @class = "txtCode" })

if you set code on the controller side the value will be put in the text box

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top