我怎样才能像 Html.CheckBoxFor()
这样自定义 HTML Helper
我制作依赖于数据库的动态表单输入,
所以我需要生成我的自定义 HTML 元素
我尝试通过 ajax 请求
返回 PartialView
并添加返回的
PartialView
的 HTML
到 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);
}
});
最佳答案
这里是一个如何制作自定义 HTML Helper 的例子
public static class HelpersExtensions
{
public static string CustomCheckBoxFor(this HtmlHelper helper, string target, string text)
{
return String.Format("<input type='checkbox' for='{0}'>{1}</input>", target, text);
}
}
注意:Html 助手无法从数据库中获取信息,只能使用通过 trought 参数传递的值。
可以做的是在 Controller 中获取信息并传递给模型,因此模型将参数传递给自定义 html 帮助器
关于javascript - 如何制作自定义 HTML Helper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57907118/