asp.net-mvc 寻找一个可以接受 MVC3 中的类属性的 Html.SubmitButton 助手

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6067874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:12:48  来源:igfitidea点击:

Looking for a Html.SubmitButton helper that would accept class attributes in MVC3

asp.net-mvc

提问by Mandy Sardona

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.

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

回答by archil

Isn't it simple just to write

是不是写起来很简单

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

<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

在 MVC 中,没有像控件这样的东西,它承载了很多应用程序逻辑。事实上,这是可能的,但不推荐。我想说的是,Html 助手只是让 Html 编写更舒适,并帮助您避免编写重复代码。在您的特定情况下,我认为直接编写 html 比使用 helper 编写更简单。但无论如何,如果您愿意,它包含在MVC Futures Library 中。方法被调用SubmitButton

回答by Mikhail

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" })

回答by Safran Ali

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

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

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

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

并且它还包含一个非常基本的 Submit helper 方法,希望对您有所帮助

回答by Luke T O'Brien

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.

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

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-csor using the TagBuilderclass: http://www.asp.net/mvc/overview/older-versions-1/views/using-the-tagbuilder-class-to-build-html-helpers-csbut I prefer to return HTMLStringthan a string

您可以按照这篇文章http://www.asp.net/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs或使用TagBuilder类来创建自己的 HTML 助手:http: //www.asp.net/mvc/overview/older-versions-1/views/using-the-tagbuilder-class-to-build-html-helpers-cs但我更喜欢返回HTMLString 而不是字符串

回答by Alexey Shevelyov

Here is how I did it.

这是我如何做到的。

Speaking of HTML 5 <button>attribute

说到 HTML 5<button>属性

Create a PartialView - call it like _HTML5Button.vbhtml

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

@ModelType YourProjectName.ButtonViewModel

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

And create a ButtonViewModel

并创建一个 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

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

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.

那么你去你部分,添加一个 id="@Model.Id" 然后在你的 PartialView 调用中你只需添加该参数 - 它不会破坏任何东西 - 如果你需要向该按钮添加一个类 - 添加它 -在一个地方,而不是搜索所有的电话。

Hope that helps - it works super well for me!

希望有帮助 - 它对我来说效果很好!

回答by Ed DeGagne

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.

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

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

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