从视图模型显示图像。多维控制器

发布于 2024-09-08 07:21:19 字数 972 浏览 4 评论 0原文

我的问题类似于 这个问题但足够不同,我觉得它需要一个单独的线程。

所以我有以下“观点”;

// A load of other calls to Model.Title, Model.Description, Model.DownloadURL 
// which work fine so no issues with actually ViewModel communication.
<fieldset>
    <div id="projectDiv">
                      // Model.ThumbnailPath is a fully qualified path to an image
                      // stored on disk, within another folder in another project.
        <img src="<%= Model.ThumbnailImagePath %>" alt="Thumbnail Image" height="100px" width="100px" />

    </div>

然而遗憾的是,我得到的只是一个轮廓白框......没有图像。

我已经调试了应用程序并拦截了发送到 ViewModel 中的文件路径变量并将其粘贴到我的计算机资源管理器中。我得到了我所期待的图像。

我已尝试将此文件路径硬编码到 的 source 属性中,但仍然是一个空的白框。

此视图页面中还有许多其他属性正在完美显示,因此通过应用程序层的通信正在运行。

有什么想法为什么这可能无法按预期发挥作用吗?

感谢您抽出时间。

My issue is similar to This question but different enough that I feel it warrants a separate thread.

So I have the following 'view';

// A load of other calls to Model.Title, Model.Description, Model.DownloadURL 
// which work fine so no issues with actually ViewModel communication.
<fieldset>
    <div id="projectDiv">
                      // Model.ThumbnailPath is a fully qualified path to an image
                      // stored on disk, within another folder in another project.
        <img src="<%= Model.ThumbnailImagePath %>" alt="Thumbnail Image" height="100px" width="100px" />

    </div>

Sadly however, all i get is an outlined white box...no image.

I have debugged the application and intercepted the file path variable thats getting sent into the ViewModel and pasted that into my computer explorer. I get the image I was expecting.

I have tried hard-coding this file path into the 's source attribute and still, an empty white box.

There are many other properties within this Viewpage that are being displayed perfectly, so communication through the application layers is working.

Any ideas why this might not function as expected ?

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身狗的梦 2024-09-15 07:21:19

src 属性必须是 URL,而不是文件路径。如果可以的话,将图像放入您的 content/images 文件夹中,并在那里引用它的 URL。如果不是,即文件需要保留在原来的位置,则考虑使用一个控制器,该控制器可以读取图像并将其作为具有正确 mime 类型的 ContentResult 返回,并使用控制器/操作/图像标识符的 url 作为图像的 src 属性。

<img src="<%= Url.Content( "~/images/thumbnails/" + Model.ThumbnailName ) %>" ...

或者

<img src="<%= Url.Action( "thumbnail", "image", Model.ImageID ) %>" ...

The src attribute needs to be a URL, not a file path. If you can, put the images in your content/images folder and reference the url to it there. If not, i.e., the file needs to stay where it is, then consider having a controller that can read the image and return it as a ContentResult with the correct mime type and use the url for the controller/action/image-identifier as the src attribute for the image.

<img src="<%= Url.Content( "~/images/thumbnails/" + Model.ThumbnailName ) %>" ...

or

<img src="<%= Url.Action( "thumbnail", "image", Model.ImageID ) %>" ...
甩你一脸翔 2024-09-15 07:21:19

虽然回复此框中的答案并不完全有意义,但我需要编写代码。

我真的不明白你会在你建议的场景 tvanfosson 的控制器端写什么。

以下是存在的内容:

MySolution.Models.Entities.Images.ProjectThumbnails 中的图像文件夹。此文件夹中存在许多 BMP,所有这些 BMP 均根据它们所属实体的 GUID 命名。

现在我有一个控制器: MySolution.Controllers.ProjectsController ,其方法如下:

 public ContentResult ProjectThumbnails(Guid Id)
 {
     ContentResult result = new ContentResult();

     Project project = projectService.GetProject(Id);
                      //This is still a fully qualified disk location path
     result.Content = project.ThumbnailImagePath; 
     return result;            
 }

然后在视图中:

<img src="<%= Url.Content(~/ProjectThumbnails/" + Model.Id) %>" />

但是,我从输入“Url.Content”时没有得到智能感知,这很少是一件好事,而且,我运行该网站时出现以下错误:

“编译器错误消息:CS1010:常量中的换行符”,错误指向该行。

你能澄清一下我在这里可能缺少什么吗?考虑到 MVC 使制作网站的大多数其他方面变得多么容易,图像处理却如此困难,这是可笑的。

While this doesn't make total sense to reply to an answer in this box, i need to write code.

I don't really understand what you would write in the controller side of things in your suggested scenario tvanfosson.

Here is what exists:

A folder of Images in: MySolution.Models.Entities.Images.ProjectThumbnails. Within this folder exists a number of bmp's all named according to the GUID of the entity they belong to.

Now I have a controller in: MySolution.Controllers.ProjectsController with a method as follows:

 public ContentResult ProjectThumbnails(Guid Id)
 {
     ContentResult result = new ContentResult();

     Project project = projectService.GetProject(Id);
                      //This is still a fully qualified disk location path
     result.Content = project.ThumbnailImagePath; 
     return result;            
 }

Then within the view:

<img src="<%= Url.Content(~/ProjectThumbnails/" + Model.Id) %>" />

However, i am getting no intellisense from typing "Url.Content" which is rarely a good thing, and also, what i run the site i get the following error:

"Compiler Error Message: CS1010: Newline in constant" With the error pointing to the line.

Could you clarify what I may be missing here ? Considering how easy MVC makes most other aspects of making websites, it is ludicrous how difficult image handling is.

指尖上的星空 2024-09-15 07:21:19

尝试使用...

<img src="@Url.Content("~/ProjectThumbnails/" + Model.Id)"/>

try using...

<img src="@Url.Content("~/ProjectThumbnails/" + Model.Id)"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文