2

I want to use jQuery UI date picker with the following date field

 <div class="editor-field">
            @Html.EditorFor(model => model.CalibrationDate)
            @Html.ValidationMessageFor(model => model.CalibrationDate)
  </div>

I have already include required script and the following function in my view

<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/jquery-ui-1.8.20.js")" type="text/javascript"></script>

<script>
    $(document).ready(function () {
        $('.date').datepicker({ dateFormat: "dd/mm/yy" }
    });

It seems that I need to assign date class to my

 @Html.EditorFor(model => model.CalibrationDate)

Can you please suggest me how to do that? or any other approach to get this functionality?


Based on comments, here is the final working code:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".date").datepicker();
});
</script> 

and in the body

<div class="editor-label">
            @Html.LabelFor(model => model.CalibrationDate)
        </div>
        <div class="editor-field">

           @Html.TextBoxFor(m => m.CalibrationDate,  new{ Class = "datepicker" })
            @Html.ValidationMessageFor(model => model.CalibrationDate)
        </div>
2

1 Answer 1

2

You can pass EditorFor an anonymous object with a class member as its additionalViewData parameter.

@Html.EditorFor(model => model.CalibrationDate, new { @class = "date" })

Note: Using an @ symbol in front of a keyword allows us to use it as an identifier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.