简体   繁体   English

使用razor修改ASP.NET MVC3中的Html Helpers

[英]Modify Html Helpers in ASP.NET MVC3 with razor

i have the following Problem: 我有以下问题:

I want to know if it is possible to modify default html helper methods, eg the Html.BeginForm() method. 我想知道是否可以修改默认的html辅助方法,例如Html.BeginForm()方法。

I know that i can write a custom helper method where i can add some stuff, but some of them have alot of overloaded functions. 我知道我可以编写一个自定义帮助器方法,我可以添加一些东西,但其中一些有很多重载函数。

then only thing i would need is, that you can add some custom html string "after" the element was rendered 那么我唯一需要的是,你可以在渲染元素之后添加一些自定义的html字符串

eg: 例如:

@using(Html.BeginForm("setDate", "DateController", new { DateId = Model.Date.Identifier }, FormMethod.Post, new { id = "setDateForm" })) {
    @* some input here... *@
}

and after the 之后

<form></form>

i would like to render a validation script, or something else, lets say jQuery validator: 我想渲染验证脚本,或其他东西,让我们说jQuery验证器:

<script>$('#setDateForm').validate();</script>

since i dont want to do that over and over again (maybe i could forget it once..) it would be nice to modify the default Html helper. 因为我不想一遍又一遍地这样做(也许我可以忘记它一次..)修改默认的Html帮助器会很好。

If it is not possible i just might have to write my own BeginForm or a wrapper for the EndForm helper :/ 如果不可能,我可能必须编写自己的BeginForm或EndForm帮助程序的包装器:/

As a very basic starting point, you could use something like this: 作为一个非常基本的起点,你可以使用这样的东西:

namespace YourProject.Helpers
{
    public static class HtmlHelperExtensions
    {
        public static IDisposable CustomBeginForm(this HtmlHelper helper, string html)
        {
            return new MvcFormExtension(helper, html);
        }

        private class MvcFormExtension : IDisposable
        {
            private HtmlHelper helper;
            private MvcForm form;
            private string html;

            public MvcFormExtension(HtmlHelper helper, string html)
            {
                this.helper = helper;
                this.form = this.helper.BeginForm();
                this.html = html;
            }

            public void Dispose()
            {
                form.EndForm();
                helper.ViewContext.Writer.Write(this.html);
            }
        }
    }
}

You'd either need to add the namespace in your view or add it to the web.config file in your Views folder. 您需要在视图中添加命名空间或将其添加到Views文件夹中的web.config文件中。 After that, you can use it like so: 之后,您可以像这样使用它:

@using (Html.CustomBeginForm("<p>test</p>")) {
    // Additional markup here
}

This works for me here but you'd certainly need to customise it to fit your needs, especially as you'll likely want to pass additional parameters to Html.BeginForm() . 这对我有用,但你肯定需要自定义它以满足你的需要,特别是因为你可能想要将其他参数传递给Html.BeginForm()

You may be able to write your own Extension method do this. 您可以编写自己的Extension方法来执行此操作。 Get the code of the BeginForm method from Codeplex.(The MVC3 source code is open source :) ) and make relevant updates to that to render the form like you want. 从Codeplex获取BeginForm方法的代码。(MVC3源代码是开源的 :))并对其进行相关更新以呈现您想要的表单。

The code is available in the FormExtensions.cs class under the System.Web.MVC Project. 该代码在System.Web.MVC项目下的FormExtensions.cs类中提供。 Look for the FormHelper method which is being called from the BeginForm Overrides. 查找从BeginForm Overrides调用的FormHelper方法。

It is not possible. 这不可能。 You'll have to do your own helpers. 你必须做自己的帮手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM