提交Fom返回400状态 [英] Submitting Fom Returns 400 Status

查看:46
本文介绍了提交Fom返回400状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图中,我正在创建一个Project.在表单中,我正在填写项目数据.该项目具有服务,对于它们,我正在使用jQuery模态形式对服务上的CRUD操作进行操作.对于CRUD操作,我使用的是AJAX.在此视图中,我有2个局部视图-在我要输入服务数据的表单上处于打开状态,而在另一个局部视图中用于更新服务数据. CRUD操作正在运行,我能够创建,更新或删除服务.我可以不用创建项目就可以做到这一点.但是,当我单击提交"按钮创建项目时,似乎没有调用正确的方法,并且无法创建项目,只是白屏了.该URL与我填写项目表单localhost:/Project/Create的URL相同.我尝试删除要在其中添加AJAX服务的jQuery,然后看起来数据已提交到项目控制器中的正确方法.这是什么原因-为什么当有jQuery代码时我无法处理表单数据?

In the view, I'm creating a Project. In the form I'm filling the project data. The project has services and for them I'm using the jQuery modal form for the CRUD operations on the Services. For CRUD operations I'm using AJAX. In this view I have 2 partial views - on for the form where I'm entering the service data and another partial view for updating the service data. The CRUD operations are working and I'm able to create, update or delete a service. I can do that without creating the project. However when I click the Submit button to create the project, it looks like the right method is no being called and I'm not able to create a project, I'm just getting white screen. The URL is the same as the one of URL where I'm filling the project form localhost:/Project/Create. I have tried deleting the jQuery where I'm adding a service with AJAX and then it looks like the data is submitted to the right method in the project controller. What is the reason for this - why I'm not able to process the form data when the jQuery code is there?

我尝试过更改AJAX调用(它始终可以正常工作,并且能够创建服务)以及BigViewModel(如此处建议的那样-

I have tried changing the AJAX call (it's always working and I'm able to create a service) as well as BigViewModel (as suggested here - Passing multiple models from View to Controller in asp MVC 5), but this is still not working...

这是控制器中的方法:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(ProjectViewModel model)
{

    int audianceId = model.ProjectModel.AudienceId;
    int categoryId = model.ProjectModel.CategoryId;
    //...............................................
    //CREATE THE PROJECT AND ADD TO DATABASE
 }

在创建服务时,这是AJAX调用(来自View):

Here is the AJAX call (from the View) when creating a service:

<script>
    var name = $("#ServiceModel_Name").val();
    var price = $("#ServiceModel_Price").val();
    var discount = $("#ServiceModel_Discount").val();
    var quantity = $("#ServiceModel_Quantity").val();
    var description = $("#ServiceModel_Description").val();
    var ir = $("#ServiceModel_IR").val();

    var details = { "name": name, "quantity": quantity, "price": price, "discount": discount, "description": description, "ir": ir };
    $.ajax({
        type: "POST",
        url: "/Project/RegisterService",
        data: details,
        datatype: "json",
        async: "true",
        success: function (response) {
            var serviceId = response;                
            $("#confirmationMessage").text("Service successfully created!");
            $(function () {
                $("#dialogMessage").dialog({
                        modal: true,
                        title: "Success!",
                        buttons: {
                            Ok: function () {
                                    $(this).dialog("close");
                                    //BindData(response);
                                    //ClearForm();
                                    $("#service1").dialog("close");
                            }
                        }
                });
            });
        },
        error: function (response) {
            alert(response.status + ' ' + response.statusText);
        }
    });
</script>

以及ProjectViewModel定义:

And the ProjectViewModel definition:

 public class ProjectViewModel
    {
        public CreateProjectViewModel ProjectModel { get; set; }

        public CreateServiceViewModel ServiceModel { get; set; }
    }

推荐答案

由于在发布操作中添加了[ValidateAntiForgeryToken]属性,因此,如果需要添加防伪验证,则需要从标头发送RequestVerificationToken.

Since you add [ValidateAntiForgeryToken] attribute on your post action,you need to send RequestVerificationToken from headers if you need yo add antiforgery validation.

1.在表单代码中添加@Html.AntiForgeryToken().

1.Add @Html.AntiForgeryToken() in your form code.

2.在ajax中添加标头:

2.Add header in your ajax:

$.ajax({
    type: "POST",
    url: "/Project/RegisterService",
    data: details,
    headers: {
        RequestVerificationToken:
            $('input:hidden[name="__RequestVerificationToken"]').val()
    }, 
    async: "true",
    success: function (response) {

    }       
});

dataType是您希望从服务器返回的内容:json,html,文本等.jQuery将使用它来找出如何填充成功函数的参数.

dataType is what you're expecting back from the server: json, html, text, etc. jQuery will use this to figure out how to populate the success function's parameter.

此外,请确保您的ajax数据与操作参数相对应.

Besides, make sure your ajax data corresponds to action parameters.

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult RegisterService(Project model)

这篇关于提交Fom返回400状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆