Durgpal Singh
Durgpal Singh

Reputation: 11953

How to get image from database

I want to access image from database direct in razor view.

This is my code

@model IEnumerable<TelerikMvcAppCombo.Models.ImageModel>

@{
    Layout = null;
}


@foreach (var image in Model)
{ 
    <img [email protected]_DESC/>
}

This is my model Class:-

[Table("IMAGESIZE")]
public class ImageModel
{
    [Key]
    public int IMAGESIZE_ID { get; set; }
    public string IMAGESIZE_NAME { get; set; }
    public string IMAGESIZE_DESC { get; set; }
    public int created_by { get; set; }
    public DateTime created_date { get; set; }
    public int modified_by { get; set; }
    public DateTime modified_date { get; set; }
}

This is my Controller Class:-

public JsonResult GetData([DataSourceRequest] DataSourceRequest request)
{
    var list = db.imageModels.ToList();
    return Json(list.ToDataSourceResult(request));
}

Upvotes: 0

Views: 233

Answers (2)

Mohsen Esmailpour
Mohsen Esmailpour

Reputation: 11544

I don't know what's your problem exactly but try this:

@foreach (var image in Model)
{ 
    <img src="@Url.Content(image.IMAGESIZE_DESC)" />
}

Upvotes: 1

dotnetstep
dotnetstep

Reputation: 17485

I don't know your view name but I assume that It is GetData. You have to return ViewResult instead of JsonResult. You are returning JsonResult so it is not working.

public ActionResult GetData([DataSourceRequest] DataSourceRequest request)
{
    var list = db.imageModels.ToList();
    return View(list.ToDataSourceResult(request));
}

If you want to use JsonResult then you have to get data client side and bind them client side and in that case Razor view is not working.

Upvotes: 0

Related Questions