Html.Editorfor losing value when rebooting the page

Asked

Viewed 48 times

4

am starting my html.editorfor with "0.00" like this:

@Html.EditorFor(model => model.Preco, new { htmlAttributes = new { @Value = "0,00"} })

However, when the user receives a validation message (Modelstate.Isvalid=false), the editor loses the value and returns to "0.00". The question is: Could I start the editor with "0.00" and after the user type the new value I keep it even if the Modelstate.Isvalid returns false?

  • And if you pass instead of passing {Value = "0.00"}, go through {Value = model.Preco.Tostring("0.00")}, then the first time is just start the model (do not pass null) and after testing the Modelstate (in httpPost) pass the same model you received by parameter back to the view

1 answer

2


There are many ways to achieve this. Perhaps the simplest is:

@Html.EditorFor(m => m.Preco, new { htmlAttributes = new { Value = Model != null? Model.Preco.ToString("0.00") : "0.00" }})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.