使用 FluentHtml (MVCContrib) 创建自定义输入元素时出现问题

发布于 2024-08-18 22:38:14 字数 1881 浏览 17 评论 0原文

我最近刚刚开始涉足 ASP.NET MVC 1.0,并遇到了精彩的 MVCContrib。我最初选择了创建一些扩展的 html 帮助器,但在找到 FluentHTML 后,我决定尝试创建自定义输入元素。基本上,我希望最终创建几个自定义输入元素,以便我正在处理的项目中的其他一些开发人员更容易将其输入字段添加到页面,并为他们呈现所有我喜欢的标记。因此,简而言之,我想用附加标记包装某些输入元素。例如,TextBox 将包装在

  • 中。
  • 我已经按照 Tim Scott 在另一个问题中的回答创建了自定义输入元素: 在 MVC 视图中进行干燥

    因此,为了进一步详细说明,我创建了我的类“TextBoxListItem”:

    public class TextBoxListItem : TextInput<TextBox>
    {
        public TextBoxListItem (string name) : base(HtmlInputType.Text, name) { }
    
        public TextBoxListItem (string name, MemberExpression forMember, IEnumerable<IBehaviorMarker> behaviors) : base(HtmlInputType.Text, name, forMember, behaviors) { }
    
        public override string ToString()
        {
            var liBuilder = new TagBuilder(HtmlTag.ListItem);
            liBuilder.InnerHtml = ToString();
            return liBuilder.ToString(TagRenderMode.SelfClosing);
        }
    }
    

    我还将它添加到我的 ViewModelContainerExtensions 类中:

    public static TextBox TextBoxListItem<T>(this IViewModelContainer<T> view, Expression<Func<T, object>> expression) where T : class
    {
        return new TextBoxListItem(expression.GetNameFor(view), expression.GetMemberExpression(), view.Behaviors)
                .Value(expression.GetValueFrom(view.ViewModel));
    }
    

    最后,我也将它添加到 ViewDataContainerExtensions 中:

    public static TextBox TextBoxListItem(this IViewDataContainer view, string name)
    {
        return new TextBox(name).Value(view.ViewData.Eval(name));
    }
    

    我在我的视图中调用它,如下所示所以:

    <%= this.TextBoxListItem("username").Label("Username:") %>
    

    无论如何,除了标准 FluentHTML TextBox 之外,我没有得到任何东西,没有包装在

  • 元素中。

    我在这里缺少什么?

    非常感谢您的任何帮助。

    I just recently started dabbling in ASP.NET MVC 1.0 and came across the wonderful MVCContrib. I had originally gone down the path of creating some extended html helpers, but after finding FluentHTML decided to try my hand at creating a custom input element. Basically I am wanting to ultimately create several custom input elements to make it easier for some other devs on the project I'm working on to add their input fields to the page and have all of my preferred markup to render for them. So, in short, I'd like to wrap certain input elements with additional markup.. A TextBox would be wrapped in an <li /> for example.

    I've created my custom input elements following Tim Scott's answer in another question on here: DRY in the MVC View.

    So, to further elaborate, I've created my class, "TextBoxListItem":

    public class TextBoxListItem : TextInput<TextBox>
    {
        public TextBoxListItem (string name) : base(HtmlInputType.Text, name) { }
    
        public TextBoxListItem (string name, MemberExpression forMember, IEnumerable<IBehaviorMarker> behaviors) : base(HtmlInputType.Text, name, forMember, behaviors) { }
    
        public override string ToString()
        {
            var liBuilder = new TagBuilder(HtmlTag.ListItem);
            liBuilder.InnerHtml = ToString();
            return liBuilder.ToString(TagRenderMode.SelfClosing);
        }
    }
    

    I've also added it to my ViewModelContainerExtensions class:

    public static TextBox TextBoxListItem<T>(this IViewModelContainer<T> view, Expression<Func<T, object>> expression) where T : class
    {
        return new TextBoxListItem(expression.GetNameFor(view), expression.GetMemberExpression(), view.Behaviors)
                .Value(expression.GetValueFrom(view.ViewModel));
    }
    

    And lastly, I've added it to ViewDataContainerExtensions as well:

    public static TextBox TextBoxListItem(this IViewDataContainer view, string name)
    {
        return new TextBox(name).Value(view.ViewData.Eval(name));
    }
    

    I'm calling it in my view like so:

    <%= this.TextBoxListItem("username").Label("Username:") %>
    

    Anyway, I'm not getting anything other than the standard FluentHTML TextBox, not wrapped in <li></li> elements.

    What am I missing here?

    Thanks very much for any assistance.

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

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

    发布评论

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

    评论(1

    溺深海 2024-08-25 22:38:14

    一切正常...

    public class TextBoxListItem : TextInput<TextBoxListItem>
    {
        public TextBoxListItem(string name) : base(HtmlInputType.Text, name) { }
    
        public TextBoxListItem(string name, MemberExpression forMember, IEnumerable<IBehaviorMarker> behaviors) : base(HtmlInputType.Text, name, forMember, behaviors) { }
    
        public override string ToString()
        {
            var liBuilder = new TagBuilder(HtmlTag.ListItem);
            liBuilder.InnerHtml = base.ToString();
            return liBuilder.ToString(TagRenderMode.Normal);
        }
    }
    public static class ViewDataContainerExtensions
    {
        public static TextBoxListItem TextBoxListItem(this IViewDataContainer view, string name)
        {
            return new TextBoxListItem(name).Value(view.ViewData.Eval(name));
        }
    }
    public static class ViewModelContainerExtensions
    {
        public static TextBoxListItem TextBoxListItem<T>(this IViewModelContainer<T> view, Expression<Func<T, object>> expression) where T : class
        {
            return new TextBoxListItem(expression.GetNameFor(view), expression.GetMemberExpression(), view.Behaviors)
                    .Value(expression.GetValueFrom(view.ViewModel));
        }
    }
    

    Everything works...

    public class TextBoxListItem : TextInput<TextBoxListItem>
    {
        public TextBoxListItem(string name) : base(HtmlInputType.Text, name) { }
    
        public TextBoxListItem(string name, MemberExpression forMember, IEnumerable<IBehaviorMarker> behaviors) : base(HtmlInputType.Text, name, forMember, behaviors) { }
    
        public override string ToString()
        {
            var liBuilder = new TagBuilder(HtmlTag.ListItem);
            liBuilder.InnerHtml = base.ToString();
            return liBuilder.ToString(TagRenderMode.Normal);
        }
    }
    public static class ViewDataContainerExtensions
    {
        public static TextBoxListItem TextBoxListItem(this IViewDataContainer view, string name)
        {
            return new TextBoxListItem(name).Value(view.ViewData.Eval(name));
        }
    }
    public static class ViewModelContainerExtensions
    {
        public static TextBoxListItem TextBoxListItem<T>(this IViewModelContainer<T> view, Expression<Func<T, object>> expression) where T : class
        {
            return new TextBoxListItem(expression.GetNameFor(view), expression.GetMemberExpression(), view.Behaviors)
                    .Value(expression.GetValueFrom(view.ViewModel));
        }
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文