在 EditorTemplates 中从 Html.TextBoxFor 检索 ClientId

发布于 2024-10-25 05:45:38 字数 1120 浏览 5 评论 0原文

我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2024-11-01 05:45:38

请参阅此问题:获取表单字段生成的 clientid ,这是我的答案:

我使用这个助手:

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)));
    }
}

就像使用任何其他助手一样使用它:@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:

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文