7

我想创建一个表单,用户可以在其中编辑个人资料并上传图片,如果他们愿意,这是我的视图

//表单字符串@using(Html.BeginForm("EditProfile","Employee",FormMethod.Post, new { enctype = "multipart/form-data " }))

//输入HTML <input type="file" name="file" id="file"/>

//提交<input type="submit" name="SubmitBtn" value="SaveProfile" /> <input type="submit" name="SubmitBtn" value="Cancel" />

现在我的控制器

    //Profile Modification
    [Authorize]
    [HttpPost]
    public ActionResult EditProfile(employer model, string SubmitBtn,HttpFileCollection file)
    {
        switch (SubmitBtn)
        {
            case "SaveProfile":
                try
                {
                    if (ModelState.IsValid)
                    {
                          //the file is always null
                        if (file != null)
                        {
                             //Function I want to Apply on file
                             model.logoname = logouploded(file);  
                            return RedirectToAction("Profile", "Employer");
                        }
                        return RedirectToAction("EditProfile");
                    }
                    //ChangeEmployeeProfile(model);

                }
                catch (Exception ex)
                {
                    ViewBag.warn = ex.Message;
                    return View(model);
                }
                break;
            case "Cancel":
                return RedirectToAction("index");
        }
        return View(model);
    }

现在无论我上传什么文件,文件总是为空,我也尝试将 HttpFileCollectionBase 作为操作函数中的参数,但文件仍然为空

只是提到员工模型只包含图像文件名,因为我只想将图像名称保存在数据库中。

4

1 回答 1

12

将 HttpFileCollection 类型更改为HttpPostedFileBase

于 2012-12-06T09:13:01.943 回答