@ Html.EditorFor(图片)

edi*_*ode 4 c# asp.net-mvc razor

我试图允许用户将图像上传到我们的网站,我不太确定如何使用它.我曾尝试使用多种类型来定义图像,包括System.Drawing.ImageHttpPostedFileWrapper@Html.EditorFor始终(可以理解)将调出其属性的字段修改.

在我看来,我确实有,但@Html.EditorFor我没有,<input type="file" name="imageToUpload" />但它没有被作为一部分的我的观点Model?我对MVC很陌生,所以我希望它是微不足道的.

这是我的观点:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>New Image</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Image)
        </div>
        <div class="editor-field">
            <input type="file" name="imageToUpload" />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
Run Code Online (Sandbox Code Playgroud)

我的控制器:

    [HttpPost]
    public ActionResult CreateImage(string brand, string collection, ImageEditViewModel imageEditViewModel)
    {
        string fileName = Guid.NewGuid().ToString();
        string serverPath = Server.MapPath("~");
        string imagesPath = serverPath + String.Format("Content\\{0}\\Images\\", Helper.Helper.ResolveBrand());

        string newLocation = Helper.Helper.SaveImage(fileName, imagesPath, imageEditViewModel.Image.InputStream)

        Image image = new Image
        {
            Collection = ds.Single<Collection>(c => c.Season == collection
                && c.Brand.Name == brand),
            Description = imageEditViewModel.Description,
            Location = "newLocation",
            Order = Helper.Helper.GetImageOrder(brand, collection)
        };

        ds.InsertOnSubmit<Image>(image);
        ds.SubmitChanges();

        return RedirectToAction("Brand");
    }
Run Code Online (Sandbox Code Playgroud)

最后是ViewModel:

public class ImageEditViewModel
{
    public int CollectionId { get; set; }

    public string Description { get; set; }

    public HttpPostedFileWrapper Image { get; set; }

    public int Order { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 15

确保enctype="multipart/form-data"在表单上指定正确的内容,否则您将无法上传文件:

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>New Image</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ImageToUpload)
        </div>
        <div class="editor-field">
            <input type="file" name="imageToUpload" />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
Run Code Online (Sandbox Code Playgroud)

如果您想使用EditorFor帮助程序生成文件输入,您可以使用以下内容:

<div class="editor-label">
    @Html.LabelFor(model => model.ImageToUpload)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.ImageToUpload)
</div>
Run Code Online (Sandbox Code Playgroud)

然后为该HttpPostedFileBase类型定义自定义编辑器模板(请参阅下文,您需要修改模型以实际使用此类型).所以编辑模板在~/Views/Shared/EditorTemplates/HttpPostedFileBase.cshtml:

@model HttpPostedFileBase
@Html.TextBox("", null, new { type = "file" })
Run Code Online (Sandbox Code Playgroud)

并在您的视图模型上使用该HttpPostedFileBase类型并确保该属性的名称与您在表单上输入的文件的名称相匹配:

public class ImageEditViewModel
{
    public int CollectionId { get; set; }

    public string Description { get; set; }

    public HttpPostedFileBase ImageToUpload { get; set; }

    public int Order { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

另外,请务必查看以下博客文章.

  • 一个大的+1,enctype很容易成为最常被遗忘的东西之一. (2认同)