I've spent most of the day chasing my tail on this. There are many wonderful suggestions out there, but none that address all my problems:
We have an existing website, where someone's linked button href's to absolute addresses, and I am trying to figure out a way to do that. I've got most of it, but one line with a styled button and formaction just won't work. The original code is like this:
@using(Html.BeginForm(null,null,FormMethod.Post))
{
// ... a <div>, then a stack of buttons, that I've converted to
// <a href="@Url.Action(.... and those buttons work
<button class="submit" id="Save" value="Save" formaction="Model/Action" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-save"></span></button>
// ... a stack of more buttons and checkboxes, that are all working...
}...
I need to retain the glyphicon as the button image, and the formaction when the button is clicked.
If I change the button to an <input>
, I lose the <span>
and hence the glyphicon.
If I call an @Url.Action()
in the button, like this:
<button type="submit" onclick="location.href='@Url.Action("Action","Model")'" class="btn.... etc.
It doesn't seem to submit anything in the post. If I specify the Action and Model in the BeginForm(), I get a resource not found, because it's trying to find the View, instead of just the action...
So, what is the proper way to change this button with the absolute link into an ASP.NET MVC something, that will fire the action in the controller and is stylable as a button with a glyphicon as image?
The issue is because of the formaction="Model/Action"
attribute on the submit button. This attribute is used to point to a different Controller and Action, than the one defined in the Html.BeginForm()
method. This is useful if you have multiple submit buttons in the same form, that should submit to different actions.
In your case, there is no Controller and Action defined in your Html.BeginForm()
method, so the formaction
attribute should point to a Controller action that accepts an argument matching the type of your model. Right now, it points "Model/Action"
. You get an HTTP 404
error, because you either don't have a controller called ModelController
, or that controller doesn't have an action called Action
. Change the controller or action part of the formaction
attribute to point to the correct controller or action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With