Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
is there any syntax issue here.I have seen the value of @item.FileName .It is the same

as the name of image in m images folder. When i tried to hard code it is working.Is there

any syntax error? The image is in image folder of the solution

**Code from View**
HTML
@foreach (var item in Model)
{
    var x = @item.FileName;
    <img src= " @Url.Content("~/Content/themes/base/images/@item.FileName") " /> 
}

**Controller Action Method**
C#
public ActionResult Index()
{
    SelectionModel sel = new SelectionModel();

    List<SelectionModel> lissel = new List<SelectionModel>();
byte[] imagedata;

    //byte[] stream ;

    string sql = "select filename from filestore";

    OracleConnection con = new OracleConnection(ConStr);
    OracleCommand com = new OracleCommand(sql, con);

    try
    {
        con.Open();   //Opening a connection
    
        OracleDataReader dr;
        dr = com.ExecuteReader();
    
        while (dr.Read())
        { 
            sel.FileName = dr[0].ToString(); //Here i am getting filename from the database      
            lissel.Add(sel);                  
        }                             
    }
    catch (Exception ex)
    {
    }

    return View(lissel);
}
Posted
v2
Comments
I guess it should be...

<img src= " @Url.Content('~/Content/themes/base/images/@item.FileName') " />

try by making change in your controller action method
C#
public ActionResult Index()
{
    SelectionModel sel = new SelectionModel();
 
    List<SelectionModel> lissel = new List<SelectionModel>();
byte[] imagedata;
 
    //byte[] stream ;

    string sql = "select filename from filestore";
 
    OracleConnection con = new OracleConnection(ConStr);
    OracleCommand com = new OracleCommand(sql, con);
 
    try
    {
        con.Open();   //Opening a connection
    
        OracleDataReader dr;
        dr = com.ExecuteReader();
    
        while (dr.Read())
        { 
            sel.FileName = "~/Content/themes/base/images/"+dr[0].ToString(); //Here i am getting filename from the database      
            lissel.Add(sel);                  
        }                             
    }
    catch (Exception ex)
    {
    }
 
    return View(lissel);
}


And make little bit change in view as
HTML
@foreach (var item in Model)
{
    <img src="@item.FileName" /> 
}
 
Share this answer
 
v2
I have found the solution .I didnt know how to use a vairable in Url.Content.The solution is



<img src= " @Url.Content("~/Content/themes/base/images/" + @item.FileName ) "/>
 
Share this answer
 
v2

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