Mr Nobody
Mr Nobody

Reputation: 389

ASP.Net MVC 5 - how to change raw html with html helper methods?

I have the following html code that I would like to change it to use html helper methods instead.

<div class="form-group col-sm-4 col-md-4 col-lg-4">
    <label>Date of Birth</label>
    <div class="input-group date" id="dtp">
        <input name="Birthday" id="txtBirthday" class="form-control" onfocus="$(this).next().trigger('click')" onkeydown="event.preventDefault()" />
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
    @Html.ValidationMessageFor(m => m.Birthday)
</div>

I want to change the above html code to the following using html helper methods but I don't know how to write onkeydown="event.preventDefault()" and $(this).next().trigger('click')" in the below code.

<div class="form-group col-sm-4 col-md-4 col-lg-4">
    @Html.LabelFor(m => m.Birthday)
    @Html.EditorFor(m => m.Birthday, new { @class = "form-control", id="txtBirthday" })    
    @Html.ValidationMessageFor(m => m.Birthday)
</div>

Upvotes: 0

Views: 288

Answers (1)

Pynt
Pynt

Reputation: 2288

You can add the onfocus and onkeydown to the same object where you added the @class = "form-control. As seen below

@Html.EditorFor(m => m.Birthday, new { @class = "form-control", id="txtBirthday", onkeydown = "event.preventDefault()", onfocus = "$(this).next().trigger('click')" })

Upvotes: 1

Related Questions