加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何在MVC中显示图像

发布时间:2020-12-16 00:07:03 所属栏目:asp.Net 来源:网络整理
导读:我已在项目的“内容/学生/照片”文件夹中成功上传照片,但无法在“索引”视图中显示这些照片. 这是我的简短代码: 模型: public string FirstName { get; set; }[Display(Name = "Upload Image")][NotMapped]public HttpPostedFileBase Photo { get; set; }
我已在项目的“内容/学生/照片”文件夹中成功上传照片,但无法在“索引”视图中显示这些照片.

这是我的简短代码:

模型:

public string FirstName { get; set; }

[Display(Name = "Upload Image")]

[NotMapped]

public HttpPostedFileBase Photo { get; set; }

索引视图(用于显示):

@Html.DisplayNameFor(model => model.FirstName)

@Html.DisplayNameFor(model => model.Photo)

@foreach (var item in Model)

{

 @Html.DisplayFor(modelItem => item.FirstName)

   @Html.DisplayFor(modelItem => item.Photo)

 }

应该在索引视图中进行哪些更改?

解决方法

尝试这个.

模型:

public byte[] ImageData { get; set; }

[HiddenInput(DisplayValue = false)]
public string ImageMimeType { get; set; }

视图:

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

@Html.EditorForModel()
    <div class="editor-label">Image</div>
        <div class="editor-field">
            @if (Model.ImageData == null) {
                @:None
            } else {
                <img width="150" height="150"
            src="@Url.Action("GetImage","Product",new { Model.ProductID })" />
            }
        <div>Upload new image: <input type="file" name="Image" /></div>
    </div>        
    <input type="submit" value="Save" />
    @Html.ActionLink("Cancel and return to List","Index")
}

控制器:

[HttpPost]
public ActionResult Edit(Product product,HttpPostedFileBase image) {
if (ModelState.IsValid) {
    if (image != null) {
        product.ImageMimeType = image.ContentType;
        product.ImageData = new byte[image.ContentLength];
        image.InputStream.Read(product.ImageData,image.ContentLength);
    }
    repository.SaveProduct(product);
    TempData["message"] = string.Format("{0} has been saved",product.Name);
    return RedirectToAction("Index");
    } else {
        // there is something wrong with the data values
        return View(product);
    }
}


public void SaveProduct(Product product) {
    if (product.ProductID == 0) {
        context.Products.Add(product);
    } else {
        Product dbEntry = context.Products.Find(product.ProductID);
        if (dbEntry != null) {
        dbEntry.Name = product.Name;
        dbEntry.Description = product.Description;
        dbEntry.Price = product.Price;
        dbEntry.Category = product.Category;
        dbEntry.ImageData = product.ImageData;
        dbEntry.ImageMimeType = product.ImageMimeType;
        }
    }
    context.SaveChanges();
}


public FileContentResult GetImage(int productId) {
    Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
    if (prod != null) {
        return File(prod.ImageData,prod.ImageMimeType);
    } else {
        return null;
    }
}

希望这可以帮助…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读