w0051977
w0051977

Reputation: 15807

Make @Html.TextBox("datepicker") readonly (so user cannot type)

Please see the View code below:

<div class="row">
    <div class="col-md-4">
        <form asp-action="MemberView">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DOB" class="control-label"></label>
                @Html.TextBox("datepicker")                   
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

How do I make @Html.TextBox("datepicker") readonly (so only the datepicker can populate it) i.e. a user cannot type in it. I have spent one hour Googling this and have already looked here: Using DatePicker with .NET

Upvotes: 0

Views: 100

Answers (1)

Matthijs
Matthijs

Reputation: 2516

Add a readonly property by passing an anonymous object as the third argument.

The null (second argument) is there to keep the input value empty. Of course you could fill it if necessary.

@Html.TextBox("datepicker", null, new { @readonly = "readonly" }) 

Note: The @ symbol is required, because 'readonly' is a reserved keyword.

Please keep in mind that the user is able to manipulate the HTML by removing the readonly attribute. Proper value checking in the back-end is required to handle the value safely.

Upvotes: 1

Related Questions