Search code examples
asp.net-mvcasp.net-coretag-helpers

How to handle two forms in one asp.net core view?


I have two forms in one view, with one ViewModel, with two submission buttons included for everyone, with two independent actions for everyone.

the problem
when I submit either one of them this error appears

An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints.
Matches:

DAS.Controllers.DocumentsController.CreateIn (DAS)
DAS.Controllers.DocumentsController.CreateEx (DAS)

The view

// one ViewModel
@model VMCreateDoc

// First form to submit "CreateIn" action
<form asp-controller="Documents" asp-action="CreateIn" enctype="multipart/form-data">
 // bla bla bla
<input type="submit" class="btn btn-success" value="Save In">
</form>

// Second form to submit "CreateEx" action
<form asp-controller="Documents" asp-action="CreateEx" enctype="multipart/form-data">
 // bla bla bla
<input type="submit" class="btn btn-success" value="Save Ex">
</form>

The actions

    // DocumentInternal Included in the same ViewModel VMCreateDoc
    [HttpPost("FileUpload")]
    public IActionResult CreateIn(DocumentInternal documentIn, List<IFormFile> files)
    {
       // bla bla bla
       return View();
    }

    // DocumentExternal Included in the same ViewModel VMCreateDoc
    [HttpPost("FileUpload")]
    public IActionResult CreateEx(DocumentExternal documentEx, List<IFormFile> files)
    {
       // bla bla bla
       return View();
    }         

The view model

public class VMCreateDoc
{
    public DocumentExternal DocEx { get; set; }
    public DocumentInternal DocIn { get; set; }
}

It seems every submission does submit for both forms. So, conflict takes place.
what do you think this problem can be solved?

Solution

  • Thanks for all guys who try to help me. and big thanks for Panagiotis Kanavos.
    the problem was with the annotation above actions.
    all I did is to change it and the problem got solved.

    like this

    // DocumentInternal Included in the same ViewModel VMCreateDoc
    [HttpPost("documentIn")]
    public IActionResult CreateIn(DocumentInternal documentIn, List<IFormFile> files)
    {
       // bla bla bla
       return View();
    }
    
    // DocumentExternal Included in the same ViewModel VMCreateDoc
    [HttpPost("documentEx")]
    public IActionResult CreateEx(DocumentExternal documentEx, List<IFormFile> files)
    {
       // bla bla bla
       return View();
    }