0
votes

I have a Edit Template Page where we EDIT, CANCEL or PREVIEW the template as per user selection. I have implemented multiple submit button for the above functionality. But the system always routing to EDIT action method.

It should route to this location "localhost/templates/Preview" instead of "localhost/templates/edit/Preview"

I am not sure whats going wrong.

Edit Action Method:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int TemplateId, TemplateViewModel templateViewModel)
    {
        try
        {

        }
        catch (Exception)
        {

            throw;
        }
    }

Preview Action Method:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Preview(TemplateViewModel templateViewModel)
    {
        try
        {

        }
        catch (Exception)
        {

            throw;
        }
    }

<form asp-controller="Templates" enctype="multipart/form-data" id="Form1">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <input type="hidden" asp-for="TemplateId" />

<table>
                        <tr>
                            <td style="padding-right:6rem">
                                <input type="submit" value="Save" name="Save" formaction="Edit" class="btn btn-primary" />
                            </td>
                            <td style="padding-right:6rem">
                                <a asp-controller="Templates" asp-action="Index" class="btn btn-primary" style="margin: 5px;">Cancel</a>
                            </td>
                            <td style="padding-right:6rem">
                                <input type="submit" value="Preview" name="Preview" formaction="Preview" class="btn btn-primary" formtarget="_blank" />
                            </td>
                        </tr>
                    </table>
                    </form>
1

1 Answers

1
votes

The formaction attribute is not an asp tag helper. You must explicitly set the address of the controller action that you want to process your request if you want to do it with the formaction attribute. For example- /Templates/Preview if you're using the [Controller]/[Action] routing.

When you've used 'Preview' as the formaction value without a leading slash it appends that to the current url you're on and sends the request there.

Another option is to have a single method that you post your requests to and to accept the name of the button as a parameter (it gets posted to the server as well). You can then use conditional logic to determine how to handle the request.