首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Html.EditorFor (图像)

@Html.EditorFor (图像)
EN

Stack Overflow用户
提问于 2012-03-13 18:14:46
回答 1查看 8.2K关注 0票数 5

我正在尝试允许用户上传图像到我们的网站,但我不太确定如何使用它。我尝试使用多种类型来定义图像,包括System.Drawing.ImageHttpPostedFileWrapper,但@Html.EditorFor总是(可以理解)将其属性显示为要编辑的字段。

在我看来,我确实有,而不是@Html.EditorFor,我有<input type="file" name="imageToUpload" />,但它没有被带到我的观点中,作为Model的一部分?我是MVC的新手,所以我希望它是一些微不足道的东西。

这是我的观点:

代码语言:javascript
复制
@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>
}

我的控制器:

代码语言:javascript
复制
    [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");
    }

最后是ViewModel:

代码语言:javascript
复制
public class ImageEditViewModel
{
    public int CollectionId { get; set; }

    public string Description { get; set; }

    public HttpPostedFileWrapper Image { get; set; }

    public int Order { get; set; }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-13 18:19:50

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

代码语言:javascript
复制
@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>
}

如果您想使用EditorFor帮助器来生成文件输入,您可以使用以下命令:

代码语言:javascript
复制
<div class="editor-label">
    @Html.LabelFor(model => model.ImageToUpload)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.ImageToUpload)
</div>

然后为HttpPostedFileBase类型定义一个自定义编辑器模板(请参见下面的内容,您需要修改模型才能实际使用此类型)。所以~/Views/Shared/EditorTemplates/HttpPostedFileBase.cshtml中的编辑器模板

代码语言:javascript
复制
@model HttpPostedFileBase
@Html.TextBox("", null, new { type = "file" })

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

代码语言:javascript
复制
public class ImageEditViewModel
{
    public int CollectionId { get; set; }

    public string Description { get; set; }

    public HttpPostedFileBase ImageToUpload { get; set; }

    public int Order { get; set; }
}

还要确保检查following blog post

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9682061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档