弹出窗口中的ajax数据发布未命中控制器方法ASP.NET MVC

问题描述 投票:3回答:1

我有一个弹出表单AddorEdit.cshtml,从index.cshtml页面调用。表单正在打开,但无法将数据发布到控制器方法。

索引页

// index.cshtml //
<a class="btn btn-success" style="margin-bottom:10px;" onclick="PopupForm('@Url.Action("AddorEdit", "Vehicles")')"><i class="fa fa-plus"></i> Add New</a>
<table id="tbl_vehicle" class="table table-striped table-bordered" style="width:100%;" >
//table block
<script>
function SubmitForm(form) {            
            $.ajax({
                type: "POST",
                url: form.action,
                data: $(form).serialize(),
                success: function (date) {
                    if(data.success)
                    {
                        Popup.dialog('close');
                        dataTable.ajax.reload();
                    }
                }
            });
</script>

弹出表格

//AddorEdit.cshtml //

@using (Html.BeginForm("AddorEdit", "Vehicles", FormMethod.Post, new { onsubmit = "return SubmitForm(this)" }))
//form

控制器方法

// VehiclesController.cs //

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddorEdit([Bind(Include = "Id,VehicleType,Amount,RenewPeriod,Status")] Vehicle vehicle)
{

        if (ModelState.IsValid)
        {
            if (vehicle.Id <= 0)
            {
                vehicle.RegisteredDate = DateTime.Now;
                vehicle.RegisteredBy = "admin";
                db.Vehicle.Add(vehicle);
            }
            else
            {
                db.Entry(vehicle).State = EntityState.Modified;
            }

            db.SaveChanges();
        }
        return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);

}

我无法检测为什么数据没有发布到控制器方法。谢谢!!!>

我有一个弹出表单AddorEdit.cshtml,它从index.cshtml页面调用。表单正在打开,但无法将数据发布到控制器方法。索引页// index.cshtml // [

c# jquery asp.net asp.net-mvc asp.net-ajax
1个回答
0
投票
use like this in your Form,it will fix your problem

    @using (Html.BeginForm("AddorEdit", "Vehicles", FormMethod.Post, new { onsubmit = 
     "return SubmitForm(this)" }))
    {
       @Html.AntiForgeryToken()
    }
© www.soinside.com 2019 - 2024. All rights reserved.