Hello. I can not deal with the subject) Here I have this property in the model:

[Required(ErrorMessage = "Поле не заполнено")] public string MyValues { get; set; } 

In the view, it is added as a hidden field:

 @Html.HiddenFor(model => model.MyValues) 

In the process of working with a form, some values ​​are added to this field. How do I output ErrorMessage for this field if nothing is added to it? I tried to do this:

 @Html.ValidationMessageFor(model => model.MyValues ) 

but something is not working.

  • 2
    but from the point of view of ordinary human logic, how do you imagine it? "You have not filled the field, which you can not see because it is hidden. Fill it at least somehow"? - DreamChild
  • I say, the values ​​in this field are added in the process of working with the form. By editing some of its other elements if you want) - Nebiross
  • so validate those fields that are visible and you will be happy - DreamChild
  • The fact is that they are not in the model, but there is a hidden field. - Nebiross
  • one
    Do you have a validation on the client or on the server? - null

1 answer 1

If you are trying to use client-side validation, check for the presence of connected scripts to use "unobtrusive" validation:

 jquery.validate.js jquery.validate.unobtrusive.js 

* when sending a form, a message should appear (although it is unlikely, but it is possible that this property does not take into account the fields that are hidden from the point of view of sound logic, see the comment to your question number 1).

You can also use server-side validation, for this the following code is needed in the controller method:

 if (string.IsNullOrEmpty(model.MyValues)) ModelState.AddModelError("MyValues", "Error: MyValues IsNullOrEmpty."); if (ModelState.IsValid) { // Field is not empty. }