剃刀基础类型/模板化剃刀代表“ using”关键字

如何解决剃刀基础类型/模板化剃刀代表“ using”关键字

| 我目前在表单中的所有输入字段中都有一个“ 0”容器,类似于:
<div class=\"ux-single-field ui-widget-content ui-corner-all\">
  @Html.LabelFor(m => m.Name)
  @Html.TextBoxFor(m => m.Name)
</div>
我想知道如何使用模板化的剃刀委托(或任何其他技巧)来封装它,就像我们使用的那样:
@using (Html.BeginForm()) {
}
我可以像这样简单地包装我的元素:
@using (Html.ContentField()) {
  @Html.LabelFor(m => m.Name)
  @Html.TextBoxFor(m => m.Name)
}
    

解决方法

使用Razor View Engine,可以使用以下方法:
namespace MyProject.Web.Helpers.Extensions
{
    public static class LayoutExtensions
    {
        public static ContentField BeginContentField(this HtmlHelper htmlHelper)
        {
            return FormHelper(htmlHelper,new RouteValueDictionary());
        }

        public static ContentField BeginContentField(this HtmlHelper htmlHelper,RouteValueDictionary htmlAttributes)
        {
            return FormHelper(htmlHelper,htmlAttributes);
        }

        public static void EndContentField(this HtmlHelper htmlHelper)
        {
            htmlHelper.ViewContext.Writer.Write(\"</div>\");
        }

        private static ContentField FormHelper(this HtmlHelper htmlHelper,IDictionary<string,object> htmlAttributes)
        {
            TagBuilder tagBuilder = new TagBuilder(\"div\");
            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute(\"class\",\"ux-single-field ui-widget-content ui-corner-all\");

            htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
            return new ContentField(htmlHelper.ViewContext.Writer);
        }
    }

    public class ContentField : IDisposable
    {
        private bool _disposed;
        private readonly TextWriter _writer;

        public ContentField(TextWriter writer)
        {
            if (writer == null)
                throw new ArgumentNullException(\"writer\");

            _writer = writer;
        }

        [SuppressMessage(\"Microsoft.Security\",\"CA2123:OverrideLinkDemandsShouldBeIdenticalToBase\")]
        public void Dispose()
        {
            Dispose(true /* disposing */);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                _disposed = true;

                _writer.Write(\"</div>\");
            }
        }

        public void EndForm()
        {
            Dispose(true);
        }
    }
}
仅供参考:使用旧的ASPX引擎,这里是操作方法。     ,接受的答案非常有帮助。我为我的项目做了一些更改和更新,我觉得对于只想参与进来并完成工作的人来说,此版本会更清晰一些。 更改包括: 升级为使用匿名类型而不是IDictionary,因为现在这已成为标准。 删除了Begin / End ...语法,因为我将只将它与using()语法一起使用,因此我觉得这更加清楚。 为清楚起见,对命名进行了调整。 添加了headerText参数,我的面板用来创建单独的div标头。如果您不需要/不需要,可以轻松删除它。 重构出几种方法。 如果您恰巧在寻找KendoUI的面板帮助程序-恰好就是这样。我有一个称为panel的类,它引用了该类,并且仅向KendoUI标签添加了边距和宽度。 使用系统; 使用System.Diagnostics.CodeAnalysis; 使用System.IO; 使用System.Web.Mvc; 命名空间MyProject.Web.HtmlHelpers.Extensions {     公共静态类LayoutExtensions     {         公共静态StyledPanel面板(此HtmlHelper htmlHelper,对象htmlAttributes = null,字符串headerText = null)         {             返回GetStyledPanel(htmlHelper,headerText,htmlAttributes);         }
    private static StyledPanel GetStyledPanel(this HtmlHelper htmlHelper,string headerText,object htmlAttributes)
    {

        if (!string.IsNullOrWhiteSpace(headerText))
            RenderHeading(htmlHelper,headerText);

        RenderDiv(htmlHelper,htmlAttributes);

        return new StyledPanel(htmlHelper.ViewContext.Writer);
    }

    private static void RenderHeading(HtmlHelper htmlHelper,string headerText)
    {
        TagBuilder tagBuilder = new TagBuilder(\"div\");
        tagBuilder.Attributes.Add(\"class\",\"panelHead\");
        tagBuilder.SetInnerText(headerText);

        htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.Normal));
    }

    private static void RenderDiv(HtmlHelper htmlHelper,object htmlAttributes) 
    {
        TagBuilder Tag = new TagBuilder(\"div\");
        Tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        Tag.MergeAttribute(\"class\",\"panel k-block k-shadow\");

        htmlHelper.ViewContext.Writer.Write(Tag.ToString(TagRenderMode.StartTag));        
    }

}

public class StyledPanel : IDisposable
{
    private bool m_Disposed;
    private readonly TextWriter m_Writer;

    public StyledPanel(TextWriter writer)
    {
        if (writer == null)
            throw new ArgumentNullException(\"Writer was null. This should never happen.\");

        m_Writer = writer;
    }

    [SuppressMessage(\"Microsoft.Security\",\"CA2123:OverrideLinkDemandsShouldBeIdenticalToBase\")]
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!m_Disposed)
        {
            m_Disposed = true;
            m_Writer.Write(\"</div>\");
        }
    }

    public void EndForm()
    {
        Dispose(true);
    }
}
} 用法可能是这样的:
@using (Html.Panel(headerText: \"My Header\",htmlAttributes: new { style = \"width: 800px;\" }))
{ 
    <table>
            <tr>
                <td class=\"label\">First Name:</td>
                <td class=\"content\"><input name=\"thing\" class=\"k-textbox\" /></td>

                <td class=\"label\">Last Name:</td>
                <td class=\"content\"><input name=\"thing\" class=\"k-textbox\" /></td>
            </tr>
    </table>
}
要不就:
@using (Html.Panel())
{ 
    <table>
            <tr>
                <td class=\"label\">First Name:</td>
                <td class=\"content\"><input name=\"thing\" class=\"k-textbox\" /></td>

                <td class=\"label\">Last Name:</td>
                <td class=\"content\"><input name=\"thing\" class=\"k-textbox\" /></td>
            </tr>
    </table>
}
    ,让我们看一看(或猜测)
Html.BeginForm()
的作用。从“渲染角度”来看,它通常只是将开始表单标记渲染到html输出中。它是一次性的,因为在这种情况下,它知道表单的内部html内容何时完成渲染,并且可以使用in10ѭ方法渲染end9ѭ标记。有了这些,您就可以得到-首先,呈现打开的
form
标签,然后显示您想要的自定义html内容,然后是end标签。结果-您在输出中获得完整的html表单。
<form>

...contents(Result of Html.TextBoxFor,etc. helpers)

</form>
我认为您的情况最好以表格形式解决。目前我没有太多时间来编写完整的代码,但是如果您通过查看源代码(感谢@druttka)反射器来查看old13ѭ(如果您有旧版本或购买了许可证)或http://wiki.sharpdevelop.net/ilspy.ashx和上述说明,您可以从头开始。从BeginForm方法中删除不必要的代码,创建MvcContentField:IDisposable类而不是MvcForm,将其上的ѭ10更改为呈现end div标签,您将完全得到所需的内容。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-