MVC traditional form of action, not calling controller method

I know this topic has been of interest to many developers and has been discussed in many forums, but I could not find the correct answer that completely solves my problem. I may not have tried very hard to find an answer, in which case it would be nice if your fellow developers could let me know about any helpful forums.

The problem I am facing is just a traditional HTML action not calling the controller's ActionResult method.

I have a partial view called "_Form.cshtml" like this:

<_Form.cshtml>

<form action="@Url.Action("Temp_Form", "Product")" method="post" class="k-content" id="tempForm">
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    <fieldset>
    <p class="lead">Please fill in the following form.</p>
    <br />

    <div class="form-horizontal">
        <div class="form-group">
            @Html.Label("Brand", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @(Html.Kendo().DropDownList()
                .Name("ddlBrand")
                .OptionLabel("--- Select ---")
                .HtmlAttributes(new { id = "brand", required = "required", data_required_msg = "Select Brand", @class = "form-control" })
                .DataTextField("Name")
                .DataValueField("Id")
                .BindTo(Products.ProductBrandCollection.LoadAll())
                )
                <span class="k-invalid-msg" data-for="ddlBrand"></span>
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Range", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @(Html.Kendo().TextBox()
                .Name("tbRange")
                .HtmlAttributes(new { required = "required", validationmessage = "Enter Range" })
                )
                <span class="k-invalid-msg" data-for="tbRange"></span>
            </div>
        </div>
    </div>

    <div id="buttondiv">
        <div class="column">
            <div class="vis" id="submitbutton">
                @(Html.Kendo().Button()
                .Name("btnSubmit")
                .HtmlAttributes(new { type = "submit", @class = "button-next" })
                .Icon("arrowhead-e")
                .Content("SUBMIT")
                .Events(e => e.Click("submit"))
                )
            </div>
        </div>
    </div>
    </fieldset>
</form>

      

In the main view "Product.cshtml" I have the following javascript click event for the submit button:

function submit(e) {
    // handles hiding and showing divs
}

      

Here is my ActionResult "Temp_Form" located in "ProductController.cs":

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Temp_Form(string ddlBrand, string tbRange)
{
    TempData["brand"] = ddlBrand;
    TempData["range"] = tbRange;
    return Content("");
}

      

I am discussing that it is either "@ Url.Action (" ActionMethod "," Controller ")" <- this part was not formatted correctly or was of both type = "submit" and ".Events (e => e.Click ("submit")) "for the submit button.

** For some reason, I am not using Html.BeginForm () to call the controller method. So my option is to use a traditional form to pass data from a view to a controller with a submit button.

I tried it too ..

<form action="myController/myAction" method="POST">

      

the above format, but my ActionResult is still not being called.

I don't think my main view is triggering the ActionResult as it only has two cascading DropDownLists that select the correct form on the client side.

Can someone really identify what I'm doing wrong or advise me on alternatives? As I mentioned above, I don't accidentally use Html.BeginForm () simply because I have those Telerik Kendo Validations that only seem to work with a traditional form.

Thank you very much in advance!

+3


source to share


2 answers


The problem is you are handling the form submission with a JavaScript event ...

So, if you want to store your JS event for the submit button click, you need to use an Ajax call from your JS method to make a POST to the action, for example:



$.ajax({
    url: '/Product/Temp_Form',
    data: $('#tempForm').serialize(),
    type: 'POST',
});

      

+2


source


You are using Razor syntax, so don't use the form tag directly, use Html.BeginForm()

as below.

@using(Html.BeginForm("Temp_Form", "Product", FormMethod.Post, new {@class = "k-content", @id="tempForm" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    <fieldset>
        //your HTML here
    </fieldset>
}

      



Also, if possible, use strongly typed models instead of loose string properties like ddlBrand and tbRange.

0


source







All Articles