This is a migrated thread and some comments may be shown as answers.

I keep getting 400 BadRequest when submitting this form

2 Answers 19 Views
Form
This is a migrated thread and some comments may be shown as answers.
Vince
Top achievements
Rank 1
Vince asked on 19 Jan 2021, 12:57 AM

Hi,

I have the following code to create a form, but when I submit it, I get a 400.

@(Html.Kendo().Form<ConfigurationModel>()
        .Name("formExample")
        .HtmlAttributes(new { url = @Url.Action("ProductQuantity", "Reports"), method = "POST" })
        .Items(items =>
        {
            items.Add()
            .Field(f => f.SchoolProductQuantityReportSchoolId)
            .Label(l => l.Text("School:"))
            .Hint("Select a school")
              .Editor(b =>
                b.ComboBox()
                .DataTextField("Description")
                .DataValueField("Id")
                .DataSource(dataSource => dataSource
                    .ServerFiltering(true)
                    .Read(read => read.Action(actionName, "School")
                    .Data("addAntiForgeryToken")))
                );
        })

My goal here is to not use Ajax, I want a full post back the old-fashioned way.

Thanks for your help.

 

2 Answers, 1 is accepted

Sort by
0
Vince
Top achievements
Rank 1
answered on 19 Jan 2021, 12:58 AM

Forgot to paste in the controller code

[HttpPost]
        [Route("Reports/ProductQuantity")]
        public async Task<IActionResult> ProductQuantity([FromForm] ConfigurationModel model)
        {
            var tmpPath = System.IO.Path.GetTempPath();
 
            var pdf = Path.Join(tmpPath, model.SchoolProductQuantityReportSchoolId.ToString());
 
            return File(await System.IO.File.ReadAllBytesAsync(pdf), MimeTypes.ApplicationPdf, "packing-slips.pdf");
        }
0
Veselin Tsvetanov
Telerik team
answered on 21 Jan 2021, 04:16 PM

Hi Vince,

Instead of url, you will need to specify the action attribute of the <form> element:

.HtmlAttributes(new { action = @Url.Action("ProductQuantity", "Reports"), method = "POST" })

The above should force the Form to submit its data to the correct endpoint. Here is some more info on the action attribute:

https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_and_retrieving_form_data#the_action_attribute

Regards,
Veselin Tsvetanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Form
Asked by
Vince
Top achievements
Rank 1
Answers by
Vince
Top achievements
Rank 1
Veselin Tsvetanov
Telerik team
Share this question
or