【问题标题】:How to display all the images stored in my MVC folder and their path is in database table using MVC razor?如何使用 MVC razor 显示存储在我的 MVC 文件夹中的所有图像,并且它们的路径在数据库表中?
【发布时间】:2016-12-21 12:22:25
【问题描述】:

这是我的控制器代码,用于将上传的图像保存在我的应用程序文件夹(“~/Content/Files”)中,并将路径存储在名为“imageupload”的数据库表中。

我的目标是使用我的数据库表中的路径显示存储在文件夹(“~/Content/Files”)中的所有图像。

        [HttpPost]
        public ActionResult Index(prop prop)
        {
            var db = new db_DotnetEntities();
            HttpPostedFileBase file = Request.Files[0];
            if (ModelState.IsValid)
            {
                var tbl = new imageupload();
                if (file == null)
                {
                    ModelState.AddModelError("File", "Please Upload file");
                }
                else if (file.ContentLength > 0)
                {
                    int MaxContentLength = 1024 * 1024 * 3;
                    string[] allowedFileExtensions = { ".jpeg", ".png", ".pdf", "gif" };
                    if (!allowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                    {
                        ModelState.AddModelError("File", "Please File of type" + string.Join(",", allowedFileExtensions));
                    }
                    else if (file.ContentLength > MaxContentLength)
                    {
                        ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
                    }
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Content/Files"), fileName);
                    tbl.imageurl = fileName;
                    db.imageuploads.Add(tbl);
                    db.SaveChanges();
                    file.SaveAs(path);
                    ModelState.AddModelError("File", "Image Sucessfully Uploaded");

                }
            }
            return View();
        }

这是我的模型

public class prop
    {
        public string fileimg { get; set; }
    }

这是我的观点

@model imageMVC.Models.prop

@using (Html.BeginForm("index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary()
    @Html.TextBoxFor(x=>x.fileimg,new{@type="File"})
    <input type="submit" id="submit"/>
}

我正在使用 ADO 数据实体模型。

请告诉我如何编写代码来显示存储在 ("~/Content/Files") 中的所有图像。

提前致谢。

【问题讨论】:

  • return View(db.imageuploads.ToList()),在您看来,在&lt;img src="" /&gt; 元素中打印文件名?你试过什么?

标签: c# razor model-view-controller


【解决方案1】:

这是我的模型。我添加了一个列表属性来获取文件夹中的图像列表(“~/Content/Files”)

    public class Prop 
        {
            public string Fileimg { get; set; }    
            public IEnumerable<string> Images { get; set; }

        } 

这是我的控制器 ActionResult 以显示文件夹中的图像列表(“~/Content/Files”)

  public ActionResult List(Prop obj)
        {
            obj.Images=Directory.EnumerateFiles(Server.MapPath("~/Content/Files/"))
                .Select(fn => "~/Content/Files/" + Path.GetFileName(fn));
            var objfile = new DirectoryInfo(Server.MapPath("~/Content/Files/"));
            var file = objfile.GetFiles("*.*");              
            return View(obj);
        }

Here is My View 循环所有图像并显示在视图中

@model imageMVC.Models.Prop

<table style="border: solid">
    <tr>
        <th>IMAGES</th>
    </tr>
    @foreach (var img in Model.Images)
    {
        <tr>
            <td>
                <img src="@Url.Content(img)"/>
            </td>

        </tr>
    }
</table>

该文件夹中的所有图像都在模型属性图像中设置,我正在循环所有图像并将其显示在视图中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-05
    • 2014-08-05
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2011-12-28
    • 2019-09-17
    • 1970-01-01
    相关资源
    最近更新 更多