从 MVC 3 Razor Html Helper 检索数据

发布于 2024-11-27 11:32:42 字数 1008 浏览 1 评论 0原文

我对正在尝试实现的 Razor Html Helper 有一个特别有趣的问题。此帮助器的目标是接受 System.Type,从该类型的属性和关联属性生成可进行 JQuery 验证的表单,并返回一个 JavaScript 字符串,该字符串将在运行时用作所述生成表单的验证规则。我通过重复调用 @Html.Raw() 来生成表单,并在迭代属性和属性时构建它。最初,html 帮助程序是在唯一要使用它的文件中编写的,但由于这是一个通用且经常使用的帮助程序,因此我将其移至项目中的 App_Code 文件夹中。

这就是问题所在:我发现当此帮助程序位于 App_Code 中时,我无法使用 ViewData 从该帮助程序返回任何数据。本来我有两个帮手,第一个是生成表单并将 JavaScript 字符串存储在 ViewData 中,第二个是获取相同的 JavaScript 字符串并将其打印到客户端脚本块中的文档中。我希望这个助手不仅能生成表单,还能一次给我这个 JavaScript 字符串,这样就不必做重复的 O(n) 工作。

原始伪代码:

@helper MakeFormAndValidationRule(Type)
{
    //generate form
    //write form using @Html.Raw()
    //generate validation rules simultaneously
    //store validation rules in ViewData
}

@helper WriteValidationRules()
{
    @Html.Raw(ViewData["rules"]);
}

<form>
    MakeFormAndValidationRule(Type)
</form>
<script>
    form.validate(@WriteValidationRules())
</script>

问题:此类情况的“最佳实践”是什么?我可以将验证规则写入 DOM 中以供以后检索,并在运行时使用 JQuery 魔法将其取出,但如果有更好、更简洁的方法来执行此操作,我想知道。

I have a particularly interesting issue with a Razor Html Helper I'm trying to implement. The goal of this helper is to accept a System.Type, generate an JQuery-validate-able form from that Type's properties and associated attributes, as well as return a JavaScript string that will be used at runtime as validation rules for said generated form. I'm generating the form by calling @Html.Raw() repeatedly, building it as I iterate through properties and attributes. Originally, the html helper was written inside the only file that was going to use it, but because this is a generalized and often-used helper, I've moved it to the App_Code folder in my project.

This is the problem: I have discovered that ViewData is not available to me to return any data from the helper when this helper is in App_Code. Originally, I had two helpers; one to generate the form and squirrel away the JavaScript string in ViewData, and a second to take that same JavaScript string and print it into the document in my client-side script block. I want this helper to not only generate the form, but also give me this JavaScript string in one pass so don't have to do duplicate O(n) work.

Original pseudocode:

@helper MakeFormAndValidationRule(Type)
{
    //generate form
    //write form using @Html.Raw()
    //generate validation rules simultaneously
    //store validation rules in ViewData
}

@helper WriteValidationRules()
{
    @Html.Raw(ViewData["rules"]);
}

<form>
    MakeFormAndValidationRule(Type)
</form>
<script>
    form.validate(@WriteValidationRules())
</script>

The question: what is the "best practice" for these sorts of cases? I could write the validation rules into the DOM for later retrieval and pull it out using JQuery magic at run-time, but if there is a better, cleaner way to do this I'd like to know.

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

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

发布评论

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

评论(2

岁吢 2024-12-04 11:32:42

我认为您想为 HtmlHelper 类创建一个 扩展方法 类。我相信这应该可以解决您的 ViewData 问题。

I think you want to create an extension method for the HtmlHelper class. I believe this should solve your issue with the ViewData issue.

云巢 2024-12-04 11:32:42

ViewData 是模型的一部分,您应该使用自定义 HTML 帮助器,而不是 Razor 帮助器。

像这样的东西:

public static MvcHtmlString MakeFormAndValidationRule<TModel>(this HtmlHelper<TModel> helper, Type type)
{
   var viewData = helper.ViewData;

   // Your code...
}

有关自定义 HTML 帮助程序的更多信息。

另外,我认为您不应该生成完整的表单并使用助手进行验证。这责任太大了。

相反,请考虑将责任分离到自定义编辑器模板。

The ViewData is part of the model, and you should use a custom HTML helper, not a Razor helper.

Something like this:

public static MvcHtmlString MakeFormAndValidationRule<TModel>(this HtmlHelper<TModel> helper, Type type)
{
   var viewData = helper.ViewData;

   // Your code...
}

More info on custom HTML helpers.

Also, i don't think you should be generating an entire form and validation with a helper. That is too much responsibility.

Instead consider segregating the responsibility to a custom editor template.

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