Click here to Skip to main content
15,877,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have uploaded profile picture of a user in a folder and saved its path in db using MVC4.
In my view i have a Image control.I want to display the profile pic of a user when he logins.

what method will be used to display image.I have get the image path from db in DisplayPic method
of a controller

[HttpPost]
public ActionResult DisplayPic(FileManagement fm)
{
string ipath;
string UserName=User.Identity.Name;
var getPath = from p in Session.Query<registration>()
where p.Email == UserName
select p;
if (getPath.Count()>; 0)
{
foreach (var imgpath in getPath)
{
ipath = imgpath.Path;
}
}

return View(fm);
}


View
@Model MvcMembership.Models.FileManagement
@{
ViewBag.Title = "DisplayPic";
}

DisplayPic


@using (Html.BeginForm())

{
<img src="" />
}

Can i pass value of image path that is stored in ipath to image src?
Any help will be appreciated.
Posted
Comments
Homero Rivera 28-Sep-13 0:19am    
Have you tried to use ASP.Net Image control? There are 2 controls available in Visual Studio, the ASP.Net Image, and regular HTML Image. The ASP.Net Image control allows you to set the "src" property on runtime. I'm not familiar with ASP.Net MVC though, I don't know if using the aforementioned is an option.

1 solution

Hi,
create 
ViewBag.Imagepath=yourpathvalue; //in controller.
Then in view,
<img src="@Url.Content(ViewBag.Imagepath)" />

Hope it helps you.
Thanks.
 
Share this answer
 
Comments
wasfasheikh 28-Sep-13 6:23am    
It is giving an error value cannot be null or empty
Harshil_Raval 28-Sep-13 7:11am    
That is because, may be you have not bind ViewBag.Imagepath value. On controller before calling to view, bind path in this viewbag.
wasfasheikh 28-Sep-13 7:49am    
I have updated the DisplayPic method

[HttpPost]
public ActionResult DisplayPic(FileManagement fm)
{


using (var session = DocumentStore.OpenSession("RavenMemberShip"))
{
string ipath;
// string UserName = User.Identity.Name;
string UserName = "wasfa_anjum@yahoo.com";
var getPath = from p in Session.Query<registration>()
where p.Email == UserName
select p;
if (getPath.Count() > 0)
{
foreach (var imgpath in getPath)
{

ipath = imgpath.Path;
ViewBag.ImagePath = ipath;
}

}

}
return View();
}
In View
<img src="@Url.Content(ViewBag.Imagepath)" alt="DisplayPicture" />
Please tell me where is the problem??
Harshil_Raval 28-Sep-13 7:52am    
hi. You are binding Viewbag value in HttpPost, that is on any postback event. Right? But when that view first time called, you have not set viewbag value, that is why it is throwing error. Hope you got me.. atleast add blank value in viewbag when calling view first time.
wasfasheikh 28-Sep-13 8:30am    
I have given a path of a picture in get method of the controller that should be displayed as first time.
[HttpGet]
public ActionResult DisplayPic()
{
ViewBag.Imagepath = "C:\\Users\\Wasfa\\Documents\\Visual Studio 2012\\Projects\\MvcMembership\\MvcMembership\\App_Data\\Uploads\\annonymous.jpg";

return View();
}
But,it is not displaying that pic.Also the pic that i want to show.I have debuged the code ipath has the correct path but i am not getting why it is not displaying images

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900