0

I have c# begin form with file upload that looks like this:

@using (Html.BeginForm("New", "Post", FormMethod.Post, new { @class = "form-inline", role = "form", id = "newPostForm", enctype = "multipart/form-data" }))
  {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(c => c.Post.MinuteID)
    @Html.HiddenFor(c => c.Post.UserID)
    @Html.TextBoxFor(m => m.PostImage, new { type = "file", name = "PostImage", id = "PostImage", accept = "image/*", @class = "file-input imageInput" })
    <button id="postBtn" type="submit" class="btn btn-primary btn-block intbtn">Post&nbsp;</button>
  }

This one perfectly works but I want to add some jquery scripts to disable the button and fields when the submit button is clicked. But after doing so, the form submitted becomes empty when it hits my controller. What am I doing wrong? This is my javascripts:

$('#postBtn').click(function () {
    $('.imageInput').prop("disabled", true);
    $('#postContent').prop("disabled", true);
    $('#btnText').text('Saving..   ');
    $('#postBtn').prop("disabled", true);
    //$('#newPostForm').submit(); -- i tried adding this and changing my button type to 'button' but still empty form values are submitted
});

The reason im doing this is to prevent the button to be clicked multiple times (multiple post), how do I fix this or if there's a better approach to prevent multiple post I'd love to take some suggestions. I want to have a loading effect though thats why I prefer javascript.. I can do this in ajax.post but I heard enctype = multipart/form-data doesnt work with ajax. Any help is appreciated.

super-user
  • 1,017
  • 4
  • 17
  • 41

1 Answers1

0

The submitted form is empty because you are disabling the inputs inside $('#postBtn').click(... handler and disabled input can't be submitted that is why you have empty form in httppost controller action.

Instead of disabling inputs just disable the submit button in order to stop multiple clicks.

Reference.

Community
  • 1
  • 1
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69