如何制作自定义 HTML Helper?

我如何制作自定义 HTML Helper 就像Html.CheckBoxFor() 我制作依赖于数据库的动态表单输入一样,所以我需要生成我的自定义 HTML 元素


我尝试PartialView通过ajax request返回并将返回HTML的添加 PartialView到DOM,但需要更多时间,具体取决于服务器端


 $.ajax({

            type: "GET",

            url: "@Url.Action("FindTransactionForInternal", "Transactions")",

            contentType: 'application/json; charset=utf-8',

            dataType: 'html',

            data: { transId:transId },

            success: function (response) {

                $('#internaldetails').html(response);

           }

        });


ITMISS
浏览 150回答 1
1回答

慕村9548890

这是如何制作自定义 HTML Helper 的示例public static class HelpersExtensions{&nbsp; &nbsp; public static string CustomCheckBoxFor(this HtmlHelper helper, string target, string text)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return String.Format("<input type='checkbox' for='{0}'>{1}</input>", target, text);&nbsp; &nbsp; }}注意: Html helper 不能从数据库中获取信息,只能使用传递给参数的值。可以做的是在控制器中获取信息并传递给模型,因此模型将参数传递给自定义 html 助手
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript