Form does not post to specified action

1 Answer 108 Views
Form
Chris
Top achievements
Rank 1
Iron
Iron
Iron
Chris asked on 28 Jun 2024, 03:00 PM

I have set up a form component and specified an action - however when debugging I can see that the form seems not to be posting to the action I have specified.

This is the form code :

@(
Html.Kendo().Form<Customer>()
        .Name("portalForm")
        .HtmlAttributes(new { action = "EditPortal", method = "POST" })
        .Items(items =>
        {
            items.Add()
                .Field(f => f.CustomerId)
                .Editor(editor => editor.Hidden());
            items.Add()
                .Field(f => f.Customer_Guid)
                .Editor(editor => editor.Hidden());
            items.Add()
                .Field(f => f.AppId)
                .Editor(editor => editor.Hidden());
            items.AddGroup()
            .Label("Settings")
            .Layout("grid")
            .Grid(g => g.Cols(4).Gutter(20))
                .Items(i =>
                {
                    i.Add()
                        .Field(f => f.CostCentresActive)
                        .Label(l => l.Text("CostCentresActive:").Optional(true))
                        .Editor(e =>
                        {
                            e.Switch()
                                .Messages(c => c.Checked("YES").Unchecked("NO"));
                        });
                    i.Add()
                        .Field(f => f.CostCentreHide)
                        .Label(l => l.Text("CostCentreHide:").Optional(true)).Editor(e =>
                        {
                            e.Switch()
                                .Messages(c => c.Checked("YES").Unchecked("NO"));
                        });
                    i.Add()
                        .Field(f => f.CustomTextActive)
                        .Label(l => l.Text("CustomTextActive:").Optional(true)).Editor(e =>
                        {
                            e.Switch()
                                .Messages(c => c.Checked("YES").Unchecked("NO"));
                        });
                    i.Add()
                        .Field(f => f.DepartmentsActive)
                        .Label(l => l.Text("DepartmentsActive:").Optional(true)).Editor(e =>
                        {
                            e.CheckBox();
                           });
                });
        })
        )

The rendered html seems to suggest it will post to the EditPortal action, but when I click submit it posts to the Edit action which is what rendered the page with the form on in the first place. 

It is a little complicated by the fact that my form component is inside a partial view, which in turn is in a tabstrip on the main Edit view. 

I am probably overlooking something very simple here!

 

Thanks

 

 

 

1 Answer, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
Iron
Iron
Iron
answered on 28 Jun 2024, 03:48 PM

I had missed that the action wasn't the MVC controller action, and so had just appended itself to the existing URL - fixed by specifying a distinct action path.

Is there a way to embed the action using Url.Action so I can specify the action and controller, and have it resolved?

Mihaela
Telerik team
commented on 03 Jul 2024, 08:34 AM

Hi Chris,

You can set the "action" form attribute by using Url.Action() method as follows:

@(Html.Kendo().Form<Customer>()
        .Name("portalForm")
        .HtmlAttributes(new { action = @Url.Action("EditPortal", "ControllerName"), method = "POST" })
        ...
)

Best,

Mihaela

Chris
Top achievements
Rank 1
Iron
Iron
Iron
commented on 03 Jul 2024, 09:22 AM

Perfect thank you - simple really! For some reason I didn't think to just mix the usual Razor syntax into the Telerik code.
Tags
Form
Asked by
Chris
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Chris
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or