0

Hi I have a dialog popup contains update form and an upload input I created update action and I added some upload codes the problem is the input file is always null in the form

Controller

[HttpPost]
public ActionResult Setting(user user, HttpPostedFileBase file)
{

View

@model Magellane.Models.user
@using (Html.BeginForm("Setting", "Account", FormMethod.Post, new { role = "form", id = "profileForm", @class = "form-horizontal", enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <input type="hidden" name="user_id" value="@Session["user_id"]">
    <div class="form-group">
        <label for="pass1" class="col-sm-2 control-label">Nom decole</label>
        <div class="col-sm-10">
            <input class="form-control-modal required" id="company" type="text" value="@Model.company" name="company" minlength=5>
            <span class="field-validation-error" data-valmsg-for="company" data-valmsg-replace="true"></span>
        </div>
    </div>
    <div class="form-group">
        <label for="pass1" class="col-sm-2 control-label">Logo</label>
            <input type="file" class="margin-none" name="logo" id="file"  onchange="readURL(this);" />
    </div>



$.ajax({
    url: '/Account/setting',
    type: 'POST',
    data: $('form').serialize(),
    dataType: 'json',
    async: false,
    success: function (xhr, status, error) {
        bootbox.alert(xhr.message);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        bootbox.alert(thrownError);
    }
});

Is there any solution?

El Hamza
  • 37
  • 2
  • 8
  • In order to post files using ajax, you need to use `FormData` and set the correct ajax options. Refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) for an example –  Dec 08 '16 at 00:28
  • Check this link for full code for transfer file [link] (http://stackoverflow.com/questions/40644898/mvc5-ajax-beginform-upload-form-with-files/40648538#40648538) – Laxman Gite Dec 08 '16 at 06:11

1 Answers1

0

Problem is your file <input> tag name=logo different than HttpPostedFileBase file variable/parameter. (i.e. logo != file)

Possible Fixes.

  • You can change the HttpPostedFileBase file to HttpPostedFileBase logo

    OR

  • You can change the <input type="file" name="file">

Note Always remember to check for the name of the control/tag because Asp.Net MVC map using the name not the id.

Vedant
  • 203
  • 1
  • 6