1

I have a razor page that uses T4template. Here is my razor code:

@model ResearchViewModel

<form method="POST" action=">

...

    @if (!Model.IsFinalized)
    {
        using (Html.BeginForm(MVC.Research.ActionNames.Reject, MVC.Research.Name, null, FormMethod.Post, new { @id = "RejectForm" }))
        {
            @Html.AntiForgeryToken()
            @Html.HiddenFor(model => model.Id)
        }
        using (Html.BeginForm(MVC.Research.ActionNames.Accept, MVC.Research.Name, null, FormMethod.Post, new { @id = "AcceptForm" }))
        {
            @Html.AntiForgeryToken()
            @Html.HiddenFor(model => model.Id)
        }
    }

    ...

</form>

The problem is that when the razor rendering this page, it cannot rendering first form !! I tried to change the sequence of these forms, and found that always the first form is not rendered. I Also tried to separate these forms using the partialview but the problem still exist. Does anyone knows that what's happening ?

  • 1
    Rather than creating multiple form tags, you can create single form with multiple different actions handled by submit buttons. Take a look for [this example](https://visualstudiomagazine.com/articles/2014/11/01/performing-multiple-actions.aspx) and [this issue](https://stackoverflow.com/questions/19650345/mvc-razor-form-with-multiple-different-submit-buttons) for implementation. – Tetsuya Yamamoto Nov 27 '18 at 01:23
  • Just to add to the other comments, the ASP.NET way was to wrap the entire document in a `
    ` tag. With Razor, you do not do it that way. If you need a single document with multiple forms, you can add each form as a distinct concern.
    – jwatts1980 Nov 28 '18 at 20:42

1 Answers1

2

You are trying to nest multiple forms and you can't do that. See this link for an explanation: Can you nest html forms?

You need to remove your starting HTML

<form method="POST" action=">

because you can't have other forms inside them. I would guess that closing tag of your first form created by razor Html helper is closing this tag, so you can see the other form created by second razor Html helper

adolja
  • 189
  • 1
  • 10