Reputation: 846
In my Razor view
. I have markup like this:
<div class="form-group">
@Html.LabelFor(model => model.Principal, new { @class = "control-label col-md-3" })
<div class="col-md-8">
@Html.EditorFor(model => model.Principal)
</div>
</div>
Principal has the Required data annotation, so my html
is rendered as so:
<input class="text-box single-line" data-val="true" data-val-number="The field Principal must be a number." data-val-required="The Principal field is required." id="Principal" name="Principal" type="text" value="0.00">
I want exactly this behavior just without the default value. Is there any way to do this without creating my own editor template?
Principal is a decimal. Making it nullable is not an option in this case, I want to force the user to input a valid number.
Upvotes: 3
Views: 457
Reputation: 6461
There is One way through Jquery
. In your model no need
to make property nullable
.
Just re-assign the value through Jquery
if your Textbox
is having default value.
$(document).ready(function () {
if($('#Principle ').val() == '0.00' || $('#Principle ').val() == '0')
{
$('#Principle ').val('');
}
});
This will be triggered when the page is once ready / Completed the loading.
As you know .Net Framework default value will be assigned when if you don't assign the value for the Value Types
.
Upvotes: 1
Reputation: 93424
I assume that Principle is a decimal type in your model. Decimal types are value types and non-nullable, thus .net must always assign a value. If you don't, it assigns the default.
If you don't want the default, you must make the property nullable.
Upvotes: 4