4

I want to create a HTML Helper For a button and I want to inject some javascript(jQuery) code with that button to my page. How I can do this?

Thanks

5
  • do you want to build script tag using HTML Helper? Commented Feb 18, 2017 at 13:18
  • @DanNguyen : I want to create HTML Helper for DropDownList and use it in scenario with cascade DropDownList Commented Feb 18, 2017 at 13:59
  • maybe you want to update your question. it said that you want yo create a button using MVC HTML Helper Commented Feb 18, 2017 at 14:02
  • @Arian use knockout and create a client side view model and bind it to the drop down lists. Should then be able to preform cascading drop down list as desired. Commented Feb 20, 2017 at 14:28
  • 2
    @Arian provide a minimal reproducible example of the desired output. this would allow for better suggestions to achieve what you require. Commented Feb 20, 2017 at 14:36

3 Answers 3

6
+400

Why don't you add a script in your htmlHelper? (of course you can add the scripts as parameters)

public static class MyHtmlHelper
{
    public static MvcHtmlString MyButton(this HtmlHelper helper,string text)
    {
        string script = @"<script> function MyMethod(){ alert('You clicked the button') ;} </script> ";
        StringBuilder html = new StringBuilder();
        html.AppendLine(script);
        html.AppendLine("<input type = 'submit' value = '"+ text + "' onclick='MyMethod()'");
        html.Append("/>");
        return new MvcHtmlString(html.ToString());
    }
}

and in your view

@Html.MyButton("Click Me")
Sign up to request clarification or add additional context in comments.

Comments

2

Suggestion is to set Id of button using Id in input tag and add JQuery/Javascript code in @scripts section. This can be done with html or using helper like this

 <input id="btnClick" type="button" value="Click Me!">

or using HtmlHelper:

 public enum ButtonType { button = 0, submit = 1, reset = 2 }

 public static class HtmlHelperExtensions
 {
    public static MvcHtmlString Button(this HtmlHelper helper, ButtonType type, string textValue, string id)
    {
               string buttonTagText = $"<input type=\"{type.ToString()}\" value=\"{textValue}\" id = \"{id}\" />";
               return MvcHtmlString.Create(buttonTagText);
    }
 }

In View file (in case extension is used)

 @Html.Button(ButtonType.button, "Click Me!", "btnClick")

Then add script as below:

@section Scripts {
   @Scripts.Render("~Scripts/buttonScript.js")
}

or add inline JS within section or refer bundled JS. In this way, JS logic can be separated from html tag, would help in getting intellisense support and easy to maintain/test code. Within Script button reference can be retrieved using

var myButton = $("#btnClick"); 

Then this reference can be used for desired script logic.

Also, ensure the script section is added in _layout.cshtml as below:

 @RenderSection("scripts", required: false)

1 Comment

I would suggest using a data attribute, if applicable, compared to an Id, as an attribute could be more re-usable. However, this pattern observes progressive enhancement, so this is the better way to go (as opposed to inlining the script).
-1

Add the below line in your view page

 @Html.Button("Click Me!", htmlAttributes:new {Onclick="OnClickfunction"})

then add the following in your script section

 <script type="text/javascript">
   function OnClickfunction()
   {
   //Write Your Code here while click.....
   }
   </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.