2

I would like to rewrite the below code using a button instead of an ActionLink. Any help would be greatly appreciated. Thanks.

@Html.ActionLink("Test Link","Index", "Home", new { id = item.id})

Is it possible to write something like the below or is there a better way ? I'm not sure about syntax either, so please feel free to correct it.

<input type="button" value="Submit" onclick="location.href='@Url.Action("Index", "Home", new { id = @Model.FirstorDefault.Id.ToString()}, null)'" />
5
  • Did you try it and found any error? Commented Apr 11, 2013 at 9:42
  • 1
    What is the purpose of using a button input? The ActionLink should be fine, you can style it with CSS to look like as a button. Commented Apr 11, 2013 at 9:49
  • No errors, but I couldn't find any data in the controller's parameter. it's all null. Yeah, I can use ActionLink, but I'm just learning MVC, so was wondering if there is anyway to overload the button. I am thinking of looking at custom helpers after this. please feel free to give me a small example for creating a custom helper like '@html.button' if possible. Thanks Commented Apr 11, 2013 at 9:58
  • "I couldn't find any data in the controller's parameter. it's all null" - what do you mean @Jundev. Is your issue more on the side of the method being called by your link or is it on the UI side? Commented Apr 11, 2013 at 10:01
  • Got it Sorted !!! I didn't include ",null" after the html attributes. That's why no data was being passed. lol..!! It's working now !! Didn't have to style anything. I've updated my answer as well. Thanks for the help. I will try your way too !! Commented Apr 11, 2013 at 10:06

2 Answers 2

7

Write it as an anchor tag instead since you seem to want to redirect users when the button is clicked. You can easily style the anchor to look like a button if you wishes to.

    <a href="@Url.Action("Index", "Home", 
        new { id = Model.FirstorDefault.Id.ToString()})">Submit</a>

And now the "Submit" caption makes no sense as you are now doing a "GET" action instead of a "POST". But if you insist on using a button you could do this:

<input type="button" value="Submit" id="btn1" />

// just replace zero with whatever value you need to use
$("#btn1").click(function () {
    window.location.replace('@Url.Action("about", "Home", new { id = 0 })');
});
Sign up to request clarification or add additional context in comments.

4 Comments

The @Html.ActionLink will render the same anchor tag.
You only downvote if the solution given is not working. Otherwise give your own solution. @laszlokiss88
Thanks. Yeah. Great idea, but I was just being a bit lazy to do styling. I'm thinking of using jquerymobile's data-role="button" feature..:-D..Just wondering, Is it possible to create a custom helper like '@Html.Button', since razor doesn't provide one ? Could you please give me a small example about how to create a simple custom helper ?
To create your onw custom Html helper this link should help you.
0
<form method="get"action="/Home/Index/@Model.id">
    <input id="Submit1" type="submit" value="submit" />
</form>

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.