Pass image from controller and display in a view using ViewBag in ASP.NET MVC 3

后端 未结 4 1862
無奈伤痛
無奈伤痛 2021-01-18 01:57

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()
        {
                   


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 02:48

    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");
    }
    

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

提交回复
热议问题