0

I am saving the user image in application domain file system. and the storing path in a database. I am getting the path in my User.PhotoPath property. I want to show the image on the page of this photopath . How do I show it?

I am using Asp.net MVC 2 + C#.

2 Answers 2

2

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>" %>
Sign up to request clarification or add additional context in comments.

Comments

2

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" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.