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>
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
jawad hasan
  • 115
  • 1
  • 8
  • 19
  • 1
    this might answer you question. [StackOver Question 4576209][1] [1]: http://stackoverflow.com/questions/4576209/asp-net-mvc-3-razor-adding-class-to-editorfor – JkenshinN Feb 25 '13 at 13:12
  • 1
    alternative way (editor template) : http://www.nickharris.net/2010/08/asp-net-mvc-editor-template-for-jquery-datepicker/ – AliRıza Adıyahşi Feb 25 '13 at 13:14

1 Answers1

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.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166