从控制器和显示器在使用ASP.NET MVC ViewBag 3期通图像 [英] Pass image from controller and display in a view using ViewBag in ASP.NET MVC 3

查看:84
本文介绍了从控制器和显示器在使用ASP.NET MVC ViewBag 3期通图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这是一件很简单的,但我不能找出如何做到这一点。在我的控制器我有:

I guess it's something very straight forward but I can't find out how to do it. In my controller I have:

 public ViewResult ShowForm()
        {
            ViewBag.Title = Resources.ApplicationTitle;
            ViewBag.LabelStatus = Resources.Status;
            //Logo
            ViewBag.Logo =@"C:\Images\Logo.png";
            return View("ShowForm");
        }

在我看来,我试试这个:

And in my view I try this:

<div id="drawForm">
<img src="@ViewBag.Logo" alt="Logo" />    
</div>

然而,当我运行这个,我刚刚得到的标志的文字。

However when I run this I just get the "Logo" text.

推荐答案

使用使用Server.Mappath 来获得图像的正确路径。假设您的图片文件夹是内容文件夹,通​​常包含在一个MVC项目中。你可以做这样的事情:

Use Server.MapPath to get the correct path of the image. Suppose your images folder is inside the Content folder that is normally included in an MVC project. You can do something like this:

public ViewResult ShowForm()
{
    //Logo
    ViewBag.Logo = Server.MapPath("~") + @"Content\Images\Logo.png";
    return View("ShowForm");
}

和你没有改变code在您的视图。

And you don't have to change the code in your view.

这篇关于从控制器和显示器在使用ASP.NET MVC ViewBag 3期通图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆