1

I am new to mvc.I created html buttons dynamically and I am unable to assign action for these buttons

Code is:

sb.Append("<li style=\"margin:17px;\">");
        sb.Append("<img src=\"" + path + "Content/HeaderSlides/FullImages/" 
                  + imagename + "\" width=\"180\" height=\"100\"/>");

        sb.Append("<input type=\"button\"  name=\"deleteimage" + id 
                  + " \" id=\"btndelete" + id 
                  + "\" value=\"Delete\" class=\"t-button t-grid-delete\" " 
                  + " style=\"margin-left:10px;\" />");

I want to make the html buttons something like this:

<input type="submit" 
       title="Delete" 
       value="Delete" 
       name="btndelete" 
       onclick="location.href='@Url.Action("DeleteHeaderSlide", 
                                           "HeaderSlides",
                                           new { filename = "Sunset.jpg" })'" />
tereško
  • 58,060
  • 25
  • 98
  • 150
Pawan
  • 1,704
  • 1
  • 16
  • 37
  • Only give complete code of creation of one button. Same will be for all others – Sami Aug 23 '12 at 12:01
  • Why are you creating html with strings? It's much better to use [helpers or templates](http://stackoverflow.com/questions/5037580/asp-net-mvc-3-partial-vs-display-template-vs-editor-template). Also, if your button simply changes the current page, why not use a link? – jrummell Aug 23 '12 at 12:32

2 Answers2

1

You'd better use jquery.

$(document).ready(function(){
    $('input[name=btndelete]').bind('click', function() {
       window.location.href='@Url.Action("DeleteHeaderSlide", 
                                       "HeaderSlides",
                                       new { filename = "Sunset.jpg" })';
    });

});
Ibrahim ULUDAG
  • 450
  • 3
  • 9
0

If you are able to create html button with your code the Simplest way to define/assign action is

string action = "<script>function yourFunction() {alert('hi');}</script>";
string btnCraeteCode = "<input type='button' onClick='yourFunction()' />";    
sub.Append(action+btnCraeteCode);

You can also have already defined yourFunction() defined in script in head tag of your page

In that case you do not need to add action string here before createButton

Sami
  • 8,168
  • 9
  • 66
  • 99