Get and display particular image along with data from database using ASP.NET MVC

515
Views
2
Answers

Table:

public int ActorID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Price { get; set; }
[DataType(DataType.ImageUrl)]
public string Image { get; set; }

My Controller code:

public ActionResult Details(int id)
{
Actor ac = db.Actors.Single(x => x.ActorID == id);
return View(ac);

View:

@model MvcApplication1.Models.Actor

@{
ViewBag.Title = "Details";
}

Details

Actor

@Html.DisplayNameFor(model => model.Name)

@Html.DisplayFor(model => model.Name)

@Html.DisplayNameFor(model => model.Description)

@Html.DisplayFor(model => model.Description)

@Html.DisplayNameFor(model => model.Price)

@Html.DisplayFor(model => model.Price)

@Html.DisplayNameFor(model => model.Image)

@@Html.DisplayFor(model => model.Image)@

@Html.ActionLink("Edit", "Edit", new { id=Model.ActorID }) |
@Html.ActionLink("Back to List", "Index")

This is my code At the time of clicking Details option It will display data correctly But ,Along with IMAGE not displaying,

please Correct my above code

edit | flag
anil babu
Asked on: Jul 12, 2013 at 12:53PM

2 Answers

167
2
167

Images should be stored as paths, you know, a string of text. For instance, a file uploaded to your site would be:

string fileStringStoredInDatabase=
System.IO.Path.Combine(Server.MapPath("~/Images"), fileName);

db.Object.ImageUrl = fileStringStoredInDatabase;

Then, you'd want to be sure the file is saved/uploaded into the /Images folder in the root directory of the web application.

To display it, on the View, you'd want to say something like:

That's a very generic explanation but it should help exponentially.

edit | flag
Codiddle Codiddle
Answered on: Aug 21, 2013 at 7:22AM
168
3
168

<img src="@Model.ImageUrl" alt="" />

Sorry, forgot to enclose in HTML format.

edit | flag
Codiddle Codiddle
Answered on: Aug 21, 2013 at 7:23AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags