2

I'm trying to send two parameters (HttpPostedFileBase and the model) from the view to the Create method in the controller but the variable HttpPostedFile photos always gets null values. Here is the Controller Code:

 public async Task <ActionResult> Create([Bind(Include = 
"Id,Name,Description")] Models.Environment environment, HttpPostedFileBase photos)
        {
            if (ModelState.IsValid)
            {
                if (photos!=null)
                {
                    DataBlobImage dataBlobImage = new DataBlobImage();
                    environment.Logo = await 
                    dataBlobImage.CreateImage("environment", 
                    environment.Id.ToString(), photos);
                }
                    //Creation date
                    environment.CreationDate = DateTime.Now;
                    //Get the creation user ID
                    environment.CreationUser = 1;
                    //By default when you create a user is active
                    environment.Active = true;
                    db.Environment.Add(environment);
                    await db.SaveChangesAsync();
                    return Json(new { success = true });
                }

                return View(environment);
            }

View code:

@using (Html.BeginForm("ModalCreate", "Environment", 
        FormMethod.Post,
        new { enctype = "multipart/form-data"     }))
{ 

        @Html.AntiForgeryToken()
        <div class="modal-header create-window">
        <button type="button" class="close" 
        data-dismiss="modal" aria-   hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Crear nuevo Ambiente</h4>
        </div>
        <div class="modal-body">

        <div class="form-horizontal">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.Name,
                 htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, 
                    new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Name, "", 
                    new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Description, 
                htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Description,
                    new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Description, "",
                    new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    <label for="file">Subir Imagen:</label>
                    <input id="photos" name="photos" type="file" 
                     style="width: 100%;" />
                </div>
             </div>

             <div class="modal-footer">
                <button class="btn" data-dismiss="modal">Cancelar</button>
                <input class="btn btn-primary" type="submit" value="Crear" />
             </div>

            </div>

        </div> 

    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Luis Rosales
  • 123
  • 1
  • 1
  • 9
  • There does not appear to be anything wrong with your code but the fact you have things like `class="modal-header` and `return Json(new { success = true });` suggests you are be using ajax to submit, in which case you need to show your script (you need to use `FormData` to serialize your form and the file for posting) –  Jul 21 '15 at 05:52
  • Actually I'm using a Bootstrap modal pop-up which I think is the reason why it doesn't work. Because when I run the code in other view it works fine. – Luis Rosales Jul 21 '15 at 22:01

1 Answers1

0

I think you don't need to pass an additional parameter for that. You can alway access it like below inside your action method:

 HttpPostedFileBase file =  Request.Files["NameOfYourFileUploadControl"];

Hope this will help.

Priyank Sheth
  • 2,352
  • 19
  • 32
  • I tried this way but It didn't work, I passed the file to my Action method this way: HttpPostedFileBase photos = Request.Files["photos"]; but still getting null value. – Luis Rosales Jul 21 '15 at 04:25
  • Can you debug Request object and see what you are getting? Check whether anything related to file is coming from client or not. – Priyank Sheth Jul 21 '15 at 04:31
  • The view that has the HttpPostedFileBase file is a partial View that uses a Bootstrap Modal Plugin. It's not getting the file from the client. But when I debugged it using the action method In my Index view It worked, However, it's not what I need. Is it a problem with the bootstrap modal plugin? – Luis Rosales Jul 21 '15 at 16:08
  • Can you visit this link which might solve your problem ? http://stackoverflow.com/questions/26563289/file-upload-in-mvc-when-used-in-bootstrap-modal-returns-null – Priyank Sheth Jul 22 '15 at 03:43