ASP.NET核心MVC。如何处理和处理请求,以表单帖子显示UI,然后发送响应

发布于 2025-02-10 03:26:05 字数 1245 浏览 0 评论 0原文

我想知道如何使用ASP.NET Core MVC实现所需的流程。情况是我有一个来自第三方的帖子请求。我需要处理该请求(过程标头等),然后显示一个UI,该UI允许用户将表单帖子提交给我的应用。然后,我的应用需要使用初始请求和用户表单帖子中的信息,以将结果返回第三方。

这是我要做的粗糙流程,但不能使一切正常,还有一个好方法吗?

步骤1:处理控制器中的初始帖子,并为用户显示视图:

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

步骤2:向用户显示视图,该视图可以使用户提交:

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

步骤3:处理提交的表单。这是不起作用的部分,因为我需要同时了解表单数据和初始请求中的信息(从步骤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);
    }

希望问题很清楚,我不知道我的问题是否试图做某事以错误的方式,或者只是找不到正确传递这些数据的正确方法。

我觉得将状态传递到视图是不对的,但这也许是最好的解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫人生路 2025-02-17 03:26:05

如果我没有误解您的问题,建议您使用以下两种方法。

第一种方法,您可以使用ViewModel包含请求信息并形式信息:

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;}
}

handleitialrequest

[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
    }

第二种方法,您可以使用tempdata [“ xxx”]在控制器操作之间传递数据:

[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);
    }

我注意到您将在最后的信息中结合有关请求和用户表格的信息,我将推荐您使用第一种方法,因为它不会再次组合信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文