寻找一个可以接受 MVC3 中的类属性的 Html.SubmitButton 帮助器

发布于 2024-11-08 03:25:35 字数 78 浏览 0 评论 0 原文

我想在 MVC3 中使用“提交”按钮的助手。有这样的东西吗?如果没有,那么有谁知道我可以在哪里获得一些代码。我想要一个允许我传递类属性的东西。

I would like to use a helper for Submit button in MVC3. Is such a thing available? If not, then does anyone know where I could get some code for this. I would like one that allows me to pass a class attribute.

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

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

发布评论

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

评论(6

埖埖迣鎅 2024-11-15 03:25:35

是不是很简单

只需编写

?在 MVC 中,不存在诸如控件之类承载大量应用程序逻辑的东西。事实上,可以但不推荐。我想说的是,Html 助手只是让 Html 编写更加舒适,并帮助您不编写重复的代码。在你的具体情况下,我认为直接编写 html 比使用 helper 编写更简单。但无论如何,如果您愿意,它包含在 MVC Futures 库 中。方法调用 SubmitButton

Isn't it simple just to write

<input type="submit" class="myclassname"/>

In MVC, there are no such things like controls, that carry much application logic. In fact, it is possible but not recommended. What i want to say is that Html helpers are just making Html writing more comfortable and help you not write duplicate code. In your particular case, i think it is simpler to write direct html than that with helper. But anyways, if you want to, it is contained in MVC Futures Library. Method is called SubmitButton

踏月而来 2024-11-15 03:25:35

只需使用以下代码添加到您的项目类:

using System.Text;

namespace System.Web.Mvc
{
public static class CustomHtmlHelper
{
    public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText, object htmlAttributes = null)
    {
        StringBuilder html = new StringBuilder();
        html.AppendFormat("<input type = 'submit' value = '{0}' ", buttonText);
        //{ class = btn btn-default, id = create-button }
        var attributes = helper.AttributeEncode(htmlAttributes);
        if (!string.IsNullOrEmpty(attributes))
        {
            attributes = attributes.Trim('{', '}');
            var attrValuePairs = attributes.Split(',');
            foreach (var attrValuePair in attrValuePairs)
            {
                var equalIndex = attrValuePair.IndexOf('=');
                var attrValue = attrValuePair.Split('=');
                html.AppendFormat("{0}='{1}' ", attrValuePair.Substring(0, equalIndex).Trim(), attrValuePair.Substring(equalIndex + 1).Trim());
            }
        }
        html.Append("/>");
        return new MvcHtmlString(html.ToString());
    }
}
}

和使用示例:

@Html.SubmitButton("Save", new { @class= "btn btn-default", id="create-button" })

Just add to your project class with such code:

using System.Text;

namespace System.Web.Mvc
{
public static class CustomHtmlHelper
{
    public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText, object htmlAttributes = null)
    {
        StringBuilder html = new StringBuilder();
        html.AppendFormat("<input type = 'submit' value = '{0}' ", buttonText);
        //{ class = btn btn-default, id = create-button }
        var attributes = helper.AttributeEncode(htmlAttributes);
        if (!string.IsNullOrEmpty(attributes))
        {
            attributes = attributes.Trim('{', '}');
            var attrValuePairs = attributes.Split(',');
            foreach (var attrValuePair in attrValuePairs)
            {
                var equalIndex = attrValuePair.IndexOf('=');
                var attrValue = attrValuePair.Split('=');
                html.AppendFormat("{0}='{1}' ", attrValuePair.Substring(0, equalIndex).Trim(), attrValuePair.Substring(equalIndex + 1).Trim());
            }
        }
        html.Append("/>");
        return new MvcHtmlString(html.ToString());
    }
}
}

And usage example:

@Html.SubmitButton("Save", new { @class= "btn btn-default", id="create-button" })
像极了他 2024-11-15 03:25:35

chk 这个链接它告诉您如何创建自定义帮助器方法,并且没有内置的提交帮助器...

http://stephenwalther.com/blog/archive/2009/03/03/chapter-6-understanding-html-helpers.aspx

它还包含一个非常基本的 Submit 帮助器方法,希望有帮助

chk this link out it tells you how to create custom helper method, and there is no builtin submit helper ...

http://stephenwalther.com/blog/archive/2009/03/03/chapter-6-understanding-html-helpers.aspx

and it also include a very basic Submit helper method, hope it helps

寻梦旅人 2024-11-15 03:25:35

按钮没有 HTML 帮助程序,因为 HTML 帮助程序反映模型的属性并通过设置正确的属性来帮助您进行绑定,它们还会查看该属性的 VilidationAttribute 元数据(如果有)并添加任何 jQuery 验证属性。

按钮不是模型的一部分,因此没有任何帮助器。

您可以按照本文 http://www.asp.net/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs 或使用 TagBuilder 类:http://www.asp.net/mvc/overview/older-versions-1/views/using-the-tagbuilder-class-to-build-html-helpers-cs但是我更喜欢返回 HTMLString 而不是字符串

There isn't a HTML helper for a button because the HTML helpers reflect over a model's propertys and help you by setting the correct attributes for binding purposes, they also look at the VilidationAttribute metadata for that property (if any) and add any jQuery validation attributes.

Buttons are not part of the model and so do not have any helpers.

You can crate your own HTML helper by following this article http://www.asp.net/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs or using the TagBuilder class: http://www.asp.net/mvc/overview/older-versions-1/views/using-the-tagbuilder-class-to-build-html-helpers-cs but I prefer to return HTMLString than a string

扶醉桌前 2024-11-15 03:25:35

虽然没有,但这不应该阻止您自己构建一个。

尽管 MVC futures 项目似乎根本没有进展或不受支持,但下载源代码并获取“提交”按钮助手(及其支持代码)来推出您自己的代码并不难。

这正是我们为大型 MVC 4 项目创建 SubmitButton 帮助程序基础的方式。

There isn't one but that shouldn't stop you from building one yourself.

Even though the MVC futures project doesn't appear to be moving forward at all or supported, it's not too hard to download the source, and grab the Submit button helper (and it's supporting code) to roll your own.

It's exactly how we created the base of our SubmitButton helper for a large MVC 4 project.

惯饮孤独 2024-11-15 03:25:35

我是这样做的。

说到 HTML 5

创建一个 PartialView - 像 _HTML5Button.vbhtml 一样调用它

@ModelType YourProjectName.ButtonViewModel

<button onclick="location.href = '@Model.URL'" class="btn btn-info">@Model.ButtonText</button>

并创建一个 ButtonViewModel

Public Class ButtonViewModel

Public URL As String
Public ButtonText As String = "Modify Button Text"
'You can add more parameters or do as you please

End Class

然后当你需要创建它时调用你的部分像这样

@Html.Partial("~/Views/Shared/MiniPartialViews/_HTML5Button.vbhtml", New ButtonViewModel With {.URL = "http://www.goanywhere.com", .ButtonText = "Name That Will Appear On The Button"})

如果你想添加更多稍后参数 - 一切都在为您集中的一个局部视图中 - 假设您想稍后添加一个 id

好吧,您转到您的局部视图,添加一个 id="@Model.Id" ,然后在您的 PartialView 中调用您添加该参数 - 它不会破坏任何内容 - 如果您需要向该按钮添加一个类 - 添加它 - 在一个地方而不是搜索所有调用。

希望有帮助——它对我来说非常有效!

Here is how I did it.

Speaking of HTML 5 <button> attribute

Create a PartialView - call it like _HTML5Button.vbhtml

@ModelType YourProjectName.ButtonViewModel

<button onclick="location.href = '@Model.URL'" class="btn btn-info">@Model.ButtonText</button>

And create a ButtonViewModel

Public Class ButtonViewModel

Public URL As String
Public ButtonText As String = "Modify Button Text"
'You can add more parameters or do as you please

End Class

Then as you need to create it call you partial like this

@Html.Partial("~/Views/Shared/MiniPartialViews/_HTML5Button.vbhtml", New ButtonViewModel With {.URL = "http://www.goanywhere.com", .ButtonText = "Name That Will Appear On The Button"})

That way if you want to add more parameters later - it is all in that one partial view centralized for you - lets say you want to add an id later on

Well you go to you partial, add an id="@Model.Id" so then in your PartialView Call you just add that parameter - it does not break anything - if you ever need to add a class to that button - add it - in one place rather than searching for all the calls.

Hope that helps - it works super well for me!

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