On my form I have a Hidden element that is strongly-typed to the viewmodel, and then I have a 'dummy' textbox where the user will enter the value that will be copied to the hidden element's value.
Razor
@Html.HiddenFor(model => model.TestProperty)
@Html.Editor("TestPropertyEmpty", new { htmlAttributes = new { @class = "form-control", @placeholder = "xxxx.x" } })
jQuery
@section scripts{
@Scripts.Render("~/bundles/jqueryval")
<script>
//enable jQuery validation on hidden elements
$.validator.setDefaults({
ignore: []
});
$(document).ready(function() {
$("#TestProperty").val(''); // clear value for validation purposes
/* Created a 'dummy' textbox so that it will be empty on page load instead of using jQuery to clear the actual strongly-typed property
because you can see the textbox being cleared on page load. So once user enters value into dummy textbox then that value will be
the value for the actual model property that is hidden
*/
$("#TestPropertyEmpty").blur(function() {
$("#TestProperty").val(this.value);
console.log($("#TestProperty").val());
});
});
</script>
}
Goal
The property TestProperty
is required and it is hidden, but I still need validation on it, hence the snippet of code above the $(document).ready...
which enables validation on hidden elements.
But my goal is that when TestProperty
is empty and the user tries to submit..
I want the 'dummy' textbox to be highlighted in red since the actual property is hidden? How do I accomplish this?
Also, is there a name for such elements that really aren't related to the model but used for these kinds of purposes like in my example?
I don't like the term 'dummy-element'.
Then I can edit my question title to represent a better, more informative style.