将表单数据发送到主控制器中的异步任务

本文关键字:控制器 异步 任务 表单 数据 | 更新日期: 2023-09-27 18:03:04

我是新的线程,我有一个异步任务,我需要张贴视图数据到这个任务。

这就是我所处的位置,我的时间不多了:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Pay(string amount, string cardExpirtyDate, string cardHolderName, string cardNumber)
    public async Task<ActionResult> PayAsync(FormCollection form)
    {
        var amount = form["amount"].ToString();
        var cardExpirtyDate = form["cardExpirtyDate"].ToString();
        var cardHolderName = form["cardHolderName"].ToString();
        var cardNumber = "4111111111111111";
        var customerId = 23;//Internal purpose to track which customer made the transaction....
        var paymentService = new PaymentService(amount, cardExpirtyDate, cardHolderName, cardNumber, customerId.ToString());
        var transactionResult = await paymentService.PostAsync();
        if (transactionResult == null) return View();
        if (transactionResult.TransactionApproved && !transactionResult.TransactionError
                && transactionResult.CustomerRef == customerId.ToString())
        {
            //transaction success....Go to payment Response and send the variables
            TempData["TRANSACTION_STATUS"] = "APPROVED";
        }
        else
        {
            var message = transactionResult.Message;
        }
        return View();
    }

它需要一个;我试着在异步任务周围添加{},也不起作用。

视图:

@using (Html.BeginForm())
    {
    @Html.AntiForgeryToken()
    <div>
        Amount:    <input type="text" name="amount" /><br />
        Expirty:   <input type="text" name="cardExpirtyDate" /><br />
        Name:   <input type="text" name="cardHolderName" /><br />
        Number:   <input type="text" name="cardNumber" /><br />
    </div>
    <input type="submit" />

    }
@if (TempData["TRANSACTION_STATUS"] != null && TempData["TRANSACTION_STATUS"] == "APPROVED")
{
    <div class="alert alert-success">Your transaction has been processed successfully. Thank you.</div>
}
else
{
    {
        <div class="alert alert-danger">There is an issue with this transaction. Please try a different card.</div>
    }
}

谢谢你看一看,道格

将表单数据发送到主控制器中的异步任务

这可能是您想要完成的。让你的主请求异步,而不是让它尝试调用另一个。

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult>  Pay(FormCollection form)
    var amount = form["amount"].ToString();
    var cardExpirtyDate = form["cardExpirtyDate"].ToString();
    var cardHolderName = form["cardHolderName"].ToString();
    var cardNumber = "4111111111111111";
    var customerId = 23;//Internal purpose to track which customer made the transaction....
    var paymentService = new PaymentService(amount, cardExpirtyDate, cardHolderName, cardNumber, customerId.ToString());
    var transactionResult = await paymentService.PostAsync();
    if (transactionResult == null) return View();
    if (transactionResult.TransactionApproved && !transactionResult.TransactionError
            && transactionResult.CustomerRef == customerId.ToString())
    {
        //transaction success....Go to payment Response and send the variables
        TempData["TRANSACTION_STATUS"] = "APPROVED";
    }
    else
    {
        var message = transactionResult.Message;
    }
    return View();
}

你有一个没有方法体的方法头:

public ActionResult Pay(string amount, string cardExpirtyDate, string cardHolderName, string cardNumber)

为方法定义一个主体,或者去掉头。

在这种情况下,编译器错误(或多个错误)可能不明显,因为在您的类中有一个非常明显的结构错误。在这种情况下,编译器根本无法理解它。

在我看来,你试图在一个方法中创建一个方法。

public ActionResult Pay(string amount, string cardExpirtyDate, string cardHolderName, string cardNumber) {
     public async Task<ActionResult> PayAsync(FormCollection form) {
            //... Your code ...
     }
}

你想要完成什么?

你应该只有一个方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> PayAsync(FormCollection form) {
   // ... Your code...
}