ASP.NET核心MVC。如何处理和处理请求,以表单帖子显示UI,然后发送响应
我想知道如何使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没有误解您的问题,建议您使用以下两种方法。
第一种方法,您可以使用ViewModel包含请求信息并形式信息:
handleitialrequest
view
handleuseraction
第二种方法,您可以使用
tempdata [“ xxx”]
在控制器操作之间传递数据:我注意到您将在最后的信息中结合有关请求和用户表格的信息,我将推荐您使用第一种方法,因为它不会再次组合信息。
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:
HandleInitialRequest
View
HandleUserAction
The second method, You can use
TempData["xxx"]
to pass data between controllers action: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.