jawad hasan
jawad hasan

Reputation: 115

ASP.NET MVC and Jquery DatePicker

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>

Upvotes: 2

Views: 6206

Answers (1)

Daniel Imms
Daniel Imms

Reputation: 50149

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.

Upvotes: 2

Related Questions