asp.net-mvc-3 - 上传图片路径名称为 "System.Web.HttpPostedFileWrapper"

标签 asp.net-mvc-3

这个工作正常吗?在某一阶段。问题是以下文本现在出现在数据库“System.Web.HttpPostedFileWrapper”中的图像字段中

以下代码来自 Controller :

  [HttpPost]
    public ActionResult Create(CarAdvert caradvert,
         HttpPostedFileBase picture1)
    {
        if (ModelState.IsValid)
        {

            if (picture1 != null)
            {
                string image1 = picture1.FileName;
                caradvert.Image1 = image1;
                var image1Path = Path.Combine(Server.MapPath("~/Content/Images"), image1);
                picture1.SaveAs(image1Path);
            }


             db.CarAdverts.Add(caradvert);
             db.SaveChanges();
             return RedirectToAction("Index");  
        }

此代码来自创建 View :

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

    @Html.ValidationSummary(true)
    <fieldset>
        <legend>CarAdvert</legend>
      <div class="editor-field">

        <input type="file" name="Image1" />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
    }

最佳答案

HttpPostedFileBase Controller 中的参数必须与输入 type="file"具有相同的名称。要么这样做:

[HttpPost]     
public ActionResult Create(CarAdvert caradvert,          
HttpPostedFileBase Image1)

<input type="file" name="picture1" />

应该可以了

关于asp.net-mvc-3 - 上传图片路径名称为 "System.Web.HttpPostedFileWrapper",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10019931/

相关文章:

asp.net-mvc-3 - MVC3 模型未从表单发回 List<CustomObject>

javascript - 表单未通过 jQuery 验证提交

asp.net-mvc-3 - Unity 为什么使用服务定位器?

c# User.IsInRole 命名空间

asp.net-mvc-3 - 如何将 “Class Library Project” 更改为 “Test Project"?

c# - jquery数据表服务器端列

asp.net - @Html.DropDownListFor 不回发到 Controller

asp.net-mvc-3 - 如何在 ASP.NET MVC 3 中更新 EF 4 实体?

asp.net - 将通用模型的子类传递给 Razor View

asp.net-mvc - 如何编写一个 MVC3/4 应用程序,既可以作为 Web API 又可以作为该 API 的 UI?