I want to make a calendar with JQuery out of an EditorTamplates. But I am struggling with getting the id of the input text coming from Html.TextBoxFor. And as I would like to have more than one calendar onto a view, I cannot assign that value directly and have to follow the context of my control.
The view :
<%@ Control
Language="C#"
Inherits="MvcContrib.FluentHtml.ModelViewUserControl<DateTimeModel>" %>
<%= Html.LabelFor(x=>x.Date) %>
<%= Html.TextBoxFor(x=>x.Date, new { @class="common-textbox-ui-calendar-tb"})%>
<script>
var CalendarTBId = '<%= this.IdFor(x=>x.Date) %>';
</script>
And the model :
public class DateTimeModel
{
public DateTime Date { get; set; }
public bool IsEnabled { get; set; }
}
So far I have tried with MvContrib, but when I would have expected to get "Filter_StartDate_Date", I receive only "Date".
Have you any bright ideas to solve that?
thanks in advance,
[Edit]
I have added the following to my view :
<% Guid ControlGuid = Guid.NewGuid(); %>
<%= this.TextBox(x=> x.Date.ToShortDateString()).Id(ControlGuid.ToString()) %>
<script>
var CalendarTBId = '<%= ControlGuid %>';
</script>
But I quite dislike it. It looks like an hack to the view to me. What do you think?
[/Edit]
See this question: get the generated clientid for a form field, this is my answer:
I use this helper:
public static partial class HtmlExtensions
{
public static MvcHtmlString ClientIdFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
return MvcHtmlString.Create(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression)));
}
}
Use it just as you would any other helper: @Html.ClientIdFor(model=>model.client.email)