views:

22

answers:

2

Hi, I am saving the user image in application domain file system. and storing path in database. I am getting the path in my User.PhotoPath property. I want to show image on page of this photopath . how to shwo it.

I am using the asp.net MVC 2 + C#. Please guide me I am new in this environment.

+2  A: 

Provide the PhotoPath as a property on your viewmodel:

public class UserViewModel
{
    //...
    public string PhotoPath { get; set; }
}

Controller:

public ActionResult Show(int id) 
{
    // load User entity from repository
    var viewModel = new UserViewModel();
    // populate viewModel from User entity
    return View(viewModel);
}

View:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<UserViewModel>" %>

...
<img src="<%= Model.PhotoPath %>" alt="User Photo" />
...

You could also pass the User entity directly to the view, but it's not recommended to do that. But if you want to do it this way, then you have to change the view's page directive accordingly:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<User>" %>
Dave
+2  A: 

Set the User as Model for the view, and the use the PhotopPath as source for the image element.

<img src="<%= model.PhotoPath %>" alt="whatever you want" />

Alternatively you can store the path within the controller in your ViewData, if you want to use another class as model for the view like this:

Controller:

ViewData["PhotoPath"] = yourUser.Photopath;

View:

<img src="<%= ViewData["PhotoPath"].ToString() %>" alt="whatever you want" />
yan.kun