自定义htmlhelper的更好解决方案,将部分脚本添加到ContentPlaceHolder

发布于 2024-10-29 04:43:01 字数 2394 浏览 3 评论 0 原文

我创建了 htmlhelper,

我一直想知道是否有一些更简洁的方法可以做到这一点。 请随意说出我做错了什么,欢迎您的建议。

    /// <summary>
    /// Custom jQuery Picker for object
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    /// <typeparam name="TProperty"></typeparam>
    /// <param name="htmlHelper"></param>
    /// <param name="expression"></param>
    /// <returns></returns>
    public static string DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
    {
      //gets name of object
      string fullHtmlFieldName = htmlHelper
                             .ViewContext
                             .ViewData
                             .TemplateInfo
                             .GetFullHtmlFieldName(
                                 ExpressionHelper.GetExpressionText(expression)
                             );
      //returns metadata for object
      var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
      DateTime date;
      DateTime.TryParse(metaData.Model.ToString(), out date); // will be of type TProperty
      StringBuilder html = new StringBuilder();
      string attributes = "";
      string objectId = fullHtmlFieldName;

      if (htmlAttributes.Count != 0)
      {
        foreach (KeyValuePair<string, object> element in htmlAttributes)
        {
          attributes += element.Key.ToLower() +"=\""+ element.Value+"\" ";
          if (element.Key.ToLower() == "id") objectId = element.Value.ToString();
        }           
      }

      html.Append("<input type=\"text\" name=\"" + fullHtmlFieldName + "\" " + attributes + " ");
      if (date != null) html.Append(" value=\"" + date.ToShortDateString() + "\"");
      html.Append("/>");
      html.Append("<script type=\"text/javascript\">$(document).ready(function() { $('#" + objectId + "').datepicker({rangeSelect: true,changeMonth: true,changeYear: true, firstDay: 1, duration: 'fast'}); });</script>");
      return html.ToString();

我还想问,是否有可能将部分代码自动解析,分配给不同的 ContentPlaceHolders

例如:代码的脚本部分将在以下 ContentPlaceHolder 中呈现:

<asp:ContentPlaceHolder ID="JavaScript" runat="server" />

I have created htmlhelper,

I have been wondering, if there is some neater way of doing this.
Please feel free to say what i am doing wrong, your suggestions are welcome.

    /// <summary>
    /// Custom jQuery Picker for object
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    /// <typeparam name="TProperty"></typeparam>
    /// <param name="htmlHelper"></param>
    /// <param name="expression"></param>
    /// <returns></returns>
    public static string DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
    {
      //gets name of object
      string fullHtmlFieldName = htmlHelper
                             .ViewContext
                             .ViewData
                             .TemplateInfo
                             .GetFullHtmlFieldName(
                                 ExpressionHelper.GetExpressionText(expression)
                             );
      //returns metadata for object
      var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
      DateTime date;
      DateTime.TryParse(metaData.Model.ToString(), out date); // will be of type TProperty
      StringBuilder html = new StringBuilder();
      string attributes = "";
      string objectId = fullHtmlFieldName;

      if (htmlAttributes.Count != 0)
      {
        foreach (KeyValuePair<string, object> element in htmlAttributes)
        {
          attributes += element.Key.ToLower() +"=\""+ element.Value+"\" ";
          if (element.Key.ToLower() == "id") objectId = element.Value.ToString();
        }           
      }

      html.Append("<input type=\"text\" name=\"" + fullHtmlFieldName + "\" " + attributes + " ");
      if (date != null) html.Append(" value=\"" + date.ToShortDateString() + "\"");
      html.Append("/>");
      html.Append("<script type=\"text/javascript\">$(document).ready(function() { $('#" + objectId + "').datepicker({rangeSelect: true,changeMonth: true,changeYear: true, firstDay: 1, duration: 'fast'}); });</script>");
      return html.ToString();

Also i want to ask, is there any possibility to assign parts of code be automatically parsed, allocated to diferent ContentPlaceHolders

eg: script part of the code will be rendered inside following ContentPlaceHolder:

<asp:ContentPlaceHolder ID="JavaScript" runat="server" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

噩梦成真你也成魔 2024-11-05 04:43:01

您可以使用模板和 Html.EditorFor(),而不是实现自定义 HtmlHelper 扩展。我已经使用它们一段时间了,没有任何问题。
以下链接可以为您提供帮助:

Instead of implementing a custom HtmlHelper extension, you can use Templating and Html.EditorFor(). I've been using them for a while with no problems.
The following links can help you:

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