0

I am learning ASP.NET MVC and I am confused between use of AJaxform or Jquery. With some answers on google I am able to understand that JQuery is a better option but still confuse why?

Here is my code for uploading file via JQuery, which is going to my controller but I am getting null for both HttpPostedFileBase file, CreatePost post

Model

 public class CreatePost
{
    public string userImage { get; set; }
}

Controller

 [HttpPost]
        public ActionResult Index(HttpPostedFileBase file, CreatePost post )
        {
            var test = post.userImage;
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
                file.SaveAs(path);
            } 

            return RedirectToAction("Index");
        }

View

    <script type="text/javascript">
    $(function () {
        $('form').submit(function () {
            if ($(this).valid()) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (result) {
                        $('#result').html(result);
                    }
                });
            }
            return false;
        });
    });

</script>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { nctype = "multipart/form-data" }))
{

     <div class="editor-label">
            @Html.LabelFor(model => model.userImage)
        </div>
        <input type="file" name="file"/>

    <input type="submit" value="OK" />
    <div id="result"></div>
}

Why I am getting null for file and post?

2 Answers 2

2

There are problems in uploading files in Ajax, I have figured that the problem is from form.Serialize() instead you can use FormData it as below:

p.s. All versions of browsers doesn't support FormData

<script type="text/javascript">
$(function () {
    $('form').submit(function () {
        //Below line is added
        var formData = new FormData($('form')[0]);
        if ($(this).valid()) {
            $.ajax({
                url: this.action,
                type: this.method,
                //Below line is changed
                data: formData,
                success: function (result) {
                    $('#result').html(result);
                }
            });
        }
        return false;
    });
});

Also there is a typo in your form declaration change ncType to enctype

if you are going to use JqueryDialog too, you can find this link useful for uploading files: Uploading file via Jquery ui dialog and Ajax [Resolved]

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot :) Just a quick question is it possible to have different jQuery for two different forms in 1 index ?
@Zerotoinfinite Do you mean two different version of Jquery?
No, I mean that what if I have two Html.BeginForm in 1 index
Yes you can, nut remember to set id for each form like new { id = 'frm1', enctype = "multipart/form-data" } and change your script to use id of the forms instead of $('form') I mean something like $('#frm1')
0

this properties are must in ajax call

processData: false, contentType: false ... your call should look like this

 $.ajax({
            url: $url,
            type: 'POST',
            data: formData,
            processData: false, 
            contentType: false,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.