How can I route to an action with an input button in asp.net mvc?
I have an inp开发者_开发百科ut button and I want to redirect to a view or action when it's clicked. How can I do this?
You can't redirect to a view. You could redirect to a controller action which in turn will render a view. You could do it using javascript:
<input type="button" id="foo" value="foo" />
and once the DOM is loaded attach a click event:
$(function() {
$('#foo').click(function() {
window.location.href = '<%= Url.Action("foo") %>';
});
});
If you are talking about a submit button then you don't need javascript, simply set the action
of the associated form
:
<% using (Html.BeginForm("actionName", "controllerName")) { %>
<input type="submit" value="foo" />
<% } %>
But I think that in your situation the most semantically correct is to simply use an anchor tag which you could style with CSS to look as a button.
Can you not just specify the appropriate URL for the action of the form that the button is part of?
I just realized that the input button needed to go to a different url that I can map with a link.
精彩评论