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" })'" />
2
  • Only give complete code of creation of one button. Same will be for all others Commented Aug 23, 2012 at 12:01
  • Why are you creating html with strings? It's much better to use helpers or templates. Also, if your button simply changes the current page, why not use a link? Commented Aug 23, 2012 at 12:32

2 Answers 2

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

});
Sign up to request clarification or add additional context in comments.

Comments

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

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.