在 EditorTemplates 中从 Html.TextBoxFor 检索 ClientId
我想用 EditorTamplates 中的 JQuery 制作日历。但我正在努力获取来自 Html.TextBoxFor 的输入文本的 id。由于我希望在一个视图上显示多个日历,因此我无法直接分配该值,并且必须遵循控件的上下文。
视图:
<%@ 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>
和模型:
public class DateTimeModel
{
public DateTime Date { get; set; }
public bool IsEnabled { get; set; }
}
到目前为止,我已经尝试过使用 MvContrib,但是当我期望得到“Filter_StartDate_Date”时,我只收到“Date”。
你有什么好主意来解决这个问题吗?
预先感谢,
[编辑]
我在我的观点中添加了以下内容:
<% Guid ControlGuid = Guid.NewGuid(); %>
<%= this.TextBox(x=> x.Date.ToShortDateString()).Id(ControlGuid.ToString()) %>
<script>
var CalendarTBId = '<%= ControlGuid %>';
</script>
但我非常不喜欢它。对我来说,这看起来像是对视图的黑客攻击。你怎么认为?
[/编辑]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅此问题:获取表单字段生成的 clientid ,这是我的答案:
我使用这个助手:
就像使用任何其他助手一样使用它:@Html.ClientIdFor(model=>model.client.email)
See this question: get the generated clientid for a form field, this is my answer:
I use this helper:
Use it just as you would any other helper: @Html.ClientIdFor(model=>model.client.email)