Reputation: 426
Hello in my project i have to apply styles for a textbox which will render Calender using Jquery and css. Am using ASP.Net MVC3 Razor
Here is my Text Box Code
<div class="col-lg-10" >@Html.EditorFor(model => model.DateOfBirth, new { @class = "form-control" })
My Template Text box which is binded with a script
<input class="form-control" type="text" id="dp1" data-date-format="mm/dd/yy" value="02/16/12"></div>
My question is how to apply the below styles for the above text box
type="text" id="dp1" data-date-format="mm/dd/yy" value="02/16/12"
Script Code
/*==DATE PICKER==*/
$('#dp1').datepicker({
format: 'mm-dd-yyyy'
});
$('#dp2').datepicker();
$('#dpYears').datepicker();
$('#dpMonths').datepicker();
});
Upvotes: 0
Views: 1454
Reputation: 22619
You could simply create your own editor template for this and reuse wherever you are required.
@Html.EditorFor(m => m.DateOfBirth, "DatePicker")
Then Views/Shared/EditorTemplates/DatePicker.cshtml
file write the below
@model DateTime?
@{
String modelValue = "";
if (Model.HasValue)
{
if (Model.Value != DateTime.MinValue)
{
modelValue = Model.Value.ToShortDateString();
}
}
}
@Html.TextBox("", modelValue, new { @class = 'my-date-picker'})
Then your CSS
.my-date-picker
{
/* your styles */
}
Javascript in a main page or _layout(if it is used across more pages)
$(document).ready(function(){
$('.my-date-picker').datepicker();
});
Upvotes: 1
Reputation: 10694
You can't set class for the EditorFor
Change
@Html.EditorFor(model => model.DateOfBirth, new { @class = "form-control" })
To
@Html.TextBoxFor(model => model.DateOfBirth, new { @class = "form-control" })
Other way to add class can be using jquery in document.ready
handler
$('#dp1').addClass('form-control');
Upvotes: 0