使用swal警报向带有参数的函数提交mvc表单

时间:2017-07-11 12:45:57

标签: javascript c# jquery asp.net-mvc

我正在尝试将表单提交给控制器。但是,我收到此错误

The parameters dictionary contains a null entry for parameter 'btnSend' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult CashDelivery(System.DateTime)' in 'TelyPayMVC_CODE.Controllers.TelecomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

控制器中的功能有3个参数,但我不知道如何提交。 这是脚本

<script>
$('#btn-submit').on('click', function (e) {
    e.preventDefault();
    var form = $(this).parents('form');
    swal({
        title: "Are you sure?",
        text: "You will not be able to recover this task!",
        type: "info",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, Send it!",
        closeOnConfirm: false
    }, function (isConfirm) {
        if (isConfirm) form.submit();
    });
});

这是函数的负责人

[HttpPost]
public ActionResult CashDelivery(DateTime btnSend, DateTime endOfPeriod, int companyID)

最后这是我需要提交的表格

using (Html.BeginForm("CashDelivery", "Telecome"))
{
  <input name="endOfPeriod" value="@pay.Item1.AddDays(6)" hidden="hidden" />
  <input name="companyID" value="1" hidden="hidden" />
   <button type="button" name="btnSend" value="@pay.Item1" class="btn btn-block btn-xs btn-primary waves-effect waves-light" id="btn-submit"> <i class="fa fa-check"></i> </button>
}

任何人都可以帮忙!

1 个答案:

答案 0 :(得分:1)

在隐藏字段中添加以下内容,不要将其设置为按钮的值

修改

查看:

@using (Html.BeginForm("CashDelivery", "Telecome"))
{
<input name="endOfPeriod" value="@pay.Item1.AddDays(6)" hidden="hidden" />
<input name="companyID" value="1" hidden="hidden" />
<input name="btnSend" value="" @pay.Item1 " hidden="hidden" />
<button type="button" name="btn-submit" value="BlahBlah" class="btn btn-block btn-xs btn-primary waves-effect waves-light" id="btn-submit"> <i class="fa fa-check"></i> </button>
}

控制器:

[HttpPost]
public ActionResult CashDelivery(FormCollection form)
{

     DateTime endOfPeriod = Convert.ToDateTime(form["endOfPeriod"]);
     DateTime btnSend = Convert.ToDateTime(form["btnSend"]);
     int companyID = Convert.ToInt32(form["companyID"]);
     //your logic goes here
}