【问题标题】:Retrieve ClientId from Html.TextBoxFor in a EditorTemplates从 EditorTemplates 中的 Html.TextBoxFor 检索 ClientId
【发布时间】:2011-03-23 10:30:58
【问题描述】:

我想用 JQuery 从 EditorTamplates 中制作一个日历。但我正在努力获取来自 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>

但我很不喜欢它。对我来说,这看起来像是对视图的破解。你怎么看?

[/编辑]

【问题讨论】:

    标签: c# jquery asp.net-mvc editortemplates


    【解决方案1】:

    看到这个问题:get the generated clientid for a form field,这是我的答案:

    我使用这个助手:

    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)

    【讨论】:

    • 非常感谢您的回答和回答,真的很有帮助!铜
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2014-05-05
    相关资源
    最近更新 更多