在自定义类中使用基类httpPostedFileWrapper时,发布的文件始终为null

时间:2019-01-11 20:56:34

标签: c# inheritance model-view-controller null

我试图覆盖httppostedfilewrapper中的SaveAs方法。在使用httppostedfilewrapper时,但在我自定义时,我已经能够成功使用以下代码

模型

public class UploadedFile : HttpPostedFileWrapper
{
    public UploadedFile(HttpPostedFile postedFile) : base(postedFile)
    {
    }

    public string Title { get; set; }
}

查看我的视图模型具有自定义类的属性(如果我在此处使用httppostedFileWrapper而不是UploadedFile,则可以正常工作

[NotMapped]
public UploadedFile File { get; set; }

在我的控制器中,它始终为空

public async Task<ActionResult> Upload(CreateProfileVM editProfile)

这是我的观点

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

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

结果有效的示例(如您所见,“文件”具有信息 enter image description here

结果不起作用的示例(此处“ File”为null),唯一的更改是View模型中的名称,或者我的自定义类始终为null,而httpPostedFilewrapper不为null ... enter image description here

我看了一些讨论该问题的链接,但我可能不太了解,或者我不知道它与我的关系。

C# MVC derived class of HttpPostedFileBase is null

https://forums.asp.net/t/1981337.aspx?C+MVC+derived+class+of+HttpPostedFileBase+is+null

任何帮助将不胜感激

我的最终目标是重写saveAs()来删除所有空格,并为发布到服务器的图像创建更自定义的自定义文件名。

当我将图像文件“ httpPostedFileWrapper”与自定义“ uploadedImage”类分开放置时,我的解决方案当前有效,在该类中,httppostedFileWrapper将图像保存,而uploadImage将信息保存到数据库(标题,文件名等)。我是在通过尝试从HttpPostedFileWrapper继承而使这个错误变得复杂吗?

0 个答案:

没有答案