0

I've a form composed by a Kendo combobox and a Kendo file upload:

@using (Html.BeginForm("Methode", "controller", FormMethod.Post))
{
    <p> Select country </p>
    @(Html.Kendo().DropDownList().Name("country")
        .DataTextField("Text").DataValueField("Value")
        .DataSource(source => { source.Read(read => {
            read.Action("GetMonth", "BudgetStage");
        });
    }))
    <p> Select a .CSV file: </p>
    @(Html.Kendo().Upload()
        .Name("files")
        .HtmlAttributes(new { accept = ".csv" })
    )
    <input type="submit" value="Submit" class="k-button k-primary" />
    <input type="button" value="Ajax" class="k-button k-primary" onclick="postData(this) 
}

Here is my C# controller code:

public JsonResult methode(IEnumerable<HttpPostedFileBase> files, string country)
{
    return Json("It's OK !", JsonRequestBehavior.AllowGet);
}

This works when I click on the submit button, the controller receives files and string values but I need to display the Json returned value from my page.

To do that, I use this ajax function:

function postData(button) {
    var form = $(button).parents('form');
    if (form.kendoValidator().data("kendoValidator").validate()) {
        $.ajax({
            type: "POST",
            url: form.attr('action'),
            data: form.serialize(),
            error: function (xhr, status, error) {
                alert(error);
            },
            success: function (response) {
                alert(response);
            }
        });
        return false;
    }
    return false;
}

When I click on the Ajax button, the backend method is call, return the Json value and my JS code display it. The problem is I receive the string value but not the selected CSV file (null).

How can I send the file and the string and display the returned Json value ?

Thank you in advance.

Vynhoû Tawa
  • 68
  • 1
  • 14

1 Answers1

0

The problem is that form.serialize() doesn't really work in this scenario. With kendo it shouldn't be different to ASP.NET MVC + JavaScript, so basically, this should help you.

Community
  • 1
  • 1
thmshd
  • 5,729
  • 3
  • 39
  • 67