我试图从视图向控制器发送两个参数,但第二个参数始终为空

时间:2015-07-21 03:47:17

标签: model-view-controller asp.net-mvc-5

我试图将两个参数(HttpPostedFileBase和模型)从视图发送到控制器中的Create方法,但变量HttpPostedFile照片总是获取空值。 这是控制器代码:

 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);
            }

查看代码:

@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> 

    }

1 个答案:

答案 0 :(得分:0)

我认为你不需要传递额外的参数。你可以在你的动作方法中随便访问它:

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

希望这会有所帮助。