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?

Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206

2 Answers2

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]

Community
  • 1
  • 1
Reza
  • 18,865
  • 13
  • 88
  • 163
  • Thanks a lot :) Just a quick question is it possible to have different jQuery for two different forms in 1 index ? – Zerotoinfinity Apr 19 '14 at 13:01
  • @Zerotoinfinite Do you mean two different version of Jquery? – Reza Apr 19 '14 at 13:06
  • No, I mean that what if I have two Html.BeginForm in 1 index – Zerotoinfinity Apr 19 '14 at 13:11
  • 1
    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')` – Reza Apr 19 '14 at 13:13
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,
chandresh patel
  • 309
  • 1
  • 10