Home > other >  ASP.NET Core MVC. How to handle and process a request, display a UI with a form POST, and then send
ASP.NET Core MVC. How to handle and process a request, display a UI with a form POST, and then send

Time:07-01

I'd like to know how I can achieve the flow I need using ASP.NET Core MVC. The situation is that I have a POST request coming in from a third party. I need to handle that request (process headers etc), then display a UI which allows the user to submit a form POST back to my app. My app then needs to use information from the initial request AND the user's form POST in order to return a result to the third party.

This is the rough flow I'm going with, but can't make everything work, is there a good way to do this?

Step 1: Handle the initial POST in a controller and display a view for the user:

    [HttpPost("initialRequest")]
    public async Task<IActionResult> HandleInitialRequest()
    {
        var state = SomeStateFromRequestToUseLater();
        var model = LoadedModelToUseInView();
        return View("UserView", model);
    }

Step 2: Display the view to the user, which has a way for the user to submit:

@model LoadedModel
...
<form method="post" asp-controller="SameController" asp-action="handleUserAction">
    <input type="text" asp-for="@Model.SomeProperty" />
...
    <button type="submit">Submit</button>
</form>

Step 3: Handle the submitted form. This is the part that isn't working, because I need to know both the form data and the information from the initial request (from step 1)

    [HttpPost("handleUserAction")]
    public async Task<IActionResult> HandleUserAction()
    {
        var state = null; // How to get this state from step 1
        var combinedModel = ModelFromStateAndFormSubmission();
        return View("SuccessView", model);
    }

Hopefully the problem is clear, I don't know if my issue is trying to do something in the wrong way, or just not finding out the right way of passing this data around.

I feel it can't be right to pass the State to the view, but maybe this is the best solution?

CodePudding user response:

If I have not misunderstood your question, I recommend you to use the following two methods.

The first method, you can use ViewModel to contains request information and form information:

public class requestModel
{
    //put the request information in this model
}

public class userForm
{
    //put the properties form needed in this model
}


public class userRequest
{
    public requestModel requestmodel {get;set;}

    public userForm userform {get;set;}
}

HandleInitialRequest

[HttpPost("initialRequest")]
    public async Task<IActionResult> HandleInitialRequest()
    {
       userRequest userrequest = new userRequest()
       {
           requestmodel = SomeStateFromRequestToUseLater();
           userform = LoadedModelToUseInView();
       }
         
        return View("UserView", userrequest);
    }

View

@model userRequest
...
<form method="post" asp-controller="SameController" asp-action="handleUserAction">
    <input type="text" asp-for="@Model.userform.SomeProperty" />
...
    <button type="submit">Submit</button>
</form>

HandleUserAction

[HttpPost("handleUserAction")]
    public async Task<IActionResult> HandleUserAction(userRequest model)
    {
        //now, userRquest contains the information about request and form,
        //SO you just need to return this model
    }

The second method, You can use TempData["xxx"] to pass data between controllers action:

[HttpPost("initialRequest")]
    public async Task<IActionResult> HandleInitialRequest()
    {
        TempData["state"] = SomeStateFromRequestToUseLater();
        var model = LoadedModelToUseInView();
        return View("UserView", model);
    }

[HttpPost("handleUserAction")]
    public async Task<IActionResult> HandleUserAction()
    {
        string state = (string)TempData["state"]; 

        var combinedModel = ModelFromStateAndFormSubmission();
        return View("SuccessView", model);
    }

I noticed that you will combine the information about request and user form in last, I perfer to recomment you to use the first method, Because it doesn't nees to combine the information again.

  • Related