Click here to Skip to main content
13,831,513 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 12 Jul 2012
Licenced CPOL

MVC extension : SubmitButton

, 12 Jul 2012
Rate this:
Please Sign up or sign in to vote.
Extension to create a submit button the clean way


I really like to work with razor views. It's clean and easy. But when I create forms, I don't like that I have to use HTML to create the submit-button. So I created an extension to create the submit button for me.

Using the Code

Regular forms with razor:

<input type="submit" value="Click me" name="buttonName" class="actionButton"/>  

Using this extension creates a cleaner form (in my opinion):

@Html.SubmitButton("buttonName", "Click me", new { @class = "actionButton" })  

The Extension Method

public static MvcHtmlString SubmitButton
(this HtmlHelper helper, string name, string text, object htmlAttributes = null)
    var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    var builder = new TagBuilder("input");

    if (htmlAttributes != null)

    builder.Attributes.Add("type", "submit");
    builder.Attributes.Add("value", text);
    builder.Attributes.Add("name", name);
    builder.Attributes.Add("id", name);
    return new MvcHtmlString(builder.ToString(TagRenderMode.SelfClosing));


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Aurum AS
Norway Norway
Microsoft Certified Solutions Developer (MCSD)

Personal website:

My projects: - A free to use norwegian crm software
Fakturax - A free to use norwegian invoice software
Timefø - A free to use norwegian timereg software
MittUtlegg - A free to use norwegian software for receipts
SupportWeb - A free to use norwegian software customersupport

You may also be interested in...

Comments and Discussions

AnswerIn tip/trick: MVC extension : SubmitButton Pin
licface131-Sep-14 23:48
memberlicface131-Sep-14 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190114.1 | Last Updated 12 Jul 2012
Article Copyright 2012 by StianSandberg
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid