1

I am trying to send some form data and file to asp.net mvc controller but i am getting error (in browser console Failed to load resource: the server responded with a status of 500 (Internal Server Error))

My code Jquery

$("#AddclientSave1").click(function () {
    alert("Handler for .click() called.");

    $("#Loading").show();

    setTimeout(3000);
    var form = $(this).serialize();
    var Dateofbirth = $('.hasDatepicker').val();
    var fd = new FormData();

    var inputs = $('#file');
    $.each(inputs, function (obj, v) {
        var file = v.files[0];
        fd.append("doc", file);
    });

    alert(fd);
    alert(Dateofbirth);

    $.ajax({
        type: "Post",
        url: '@Url.Action("AddClient", "TEST")',

        data: { formData: form, doc: fd, Dateofbirth: Dateofbirth },
        cache: false,
        contentType: false,
        processData: false,
        async: false,
        success: function (data) {
            if (data) {
                $("#Loading").hide();
                $(".AddclientN").empty();
                $(".AddclientN").html(data);
            }
        }, error: function (xhr, status, error) {
            $("#Loading").hide();
        }
    });

});

C# controller

[HttpPost]
public ActionResult AddClient(Clients formData, IEnumerable<HttpPostedFileBase> doc, DateTime Dateofbirth)
{
    return View(new Clients());
}
ZCoder
  • 2,155
  • 5
  • 25
  • 62
  • 2
    why are you returning `View` for an ajax? – adiga Sep 18 '17 at 17:56
  • I have method which i am calling there to save data _actionplanRepository.AddClient(client,doc, Dateofbirth); currently make it simple i just need the form and file to show up in parameters – ZCoder Sep 18 '17 at 18:09
  • 1
    If you are returning a `View`, why not do a regular form submit without the ajax? (BTW `setTimeout(3000);` won't do anything. It doesn't behave like `Thread.Sleep`) – adiga Sep 18 '17 at 18:14
  • because I am updating Div when data successfully saved or not saved this not whole logic I removed the logic..to make things simple for people who read this question. – ZCoder Sep 18 '17 at 18:25
  • my main issue is when i am trying to send data from jquery to controller i having issue – ZCoder Sep 18 '17 at 18:25
  • You're getting an error because of `return View()`. If you only want to update a div upon success, Just `return Json(true)` instead of `View` – adiga Sep 18 '17 at 18:36
  • And don't use `async: false`. It was deprecated more than 5 years ago, not to mention it freezes your screen until a response is received. – adiga Sep 18 '17 at 18:55

0 Answers0