ModelState验证-actionresult触发两次

时间:2018-06-29 00:08:17

标签: c# asp.net-mvc model-validation

我有一个提交异步任务的结帐页面。如果用户提交干净的表单(没有验证错误),则控制器将正确地进行迭代,并对其需要执行的各个端点进行所有正确的调用。但是,如果用户未能填写必填字段,则控制器将重复操作结果。似乎没有人按几次提交带有验证的次数,它总是会触发两次。

这是控制器:

public async Task<ActionResult> PostCheckoutForm(CheckoutForm checkoutForm)
    {
        if (!ModelState.IsValid)
        {
            return CurrentUmbracoPage();
        };

        var basket = await TransactionLibrary.GetBasketAsync();

        switch (checkoutForm.SelectedPayment)
         {
             case PaymentType.Card:
        var restClient = new WorldpayRestClient("https://api.worldpay.com/v1", _config.WorldPayServiceKey);

        try
        {
            var sentToCrm = await _orderService.SaveOpportunity(CreateRequest(basket, checkoutForm));

sendToCrm等待任务触发两次,从而在我们的CRM中创建了两个机会,但是再次触发一次,没有任何验证问题。

这是视图:

<div class="checkout" data-component="components/checkout" data-checkout-client-id="@Model.ClientId">

            @using (Html.BeginUmbracoForm<CheckoutPageSurfaceController>("PostCheckoutForm", FormMethod.Post, new { id = "checkout__form" }))
{
        @Html.ValidationSummary(false)

        <section class="checkout__details">
            <div class="checkout__input-cells">
                <div class="checkout__input-cell">
                    @Html.LabelFor(m => m.FirstName)*
                    @Html.TextBoxFor(m => m.FirstName)
                </div>

<input class="checkout__payment-button button" type="submit" name="process" id="card_payment" value="Submit Payment" onclick='Worldpay.submitTemplateForm()' data-payment-type="@PaymentType.Card" />

以下是该表格的代码段

public class CheckoutForm
{
    public string ClientId { get; set; }


    [Required(AllowEmptyStrings = false, ErrorMessage = "Please enter a first name"), Display(Name = "First Name")]
    public string FirstName { get; set; }

有什么想法会引起问题吗?

编辑:Worldpay API操作(用于单击):

Worldpay.template.submitTemplateForm=function(){
return Worldpay.callbacks.beforeSubmit()?void 
Worldpay.template.sendMessage(Worldpay.helpers.JSON.stringify({
submit:!0})):!1}

0 个答案:

没有答案