图像未显示在使用 ASP.NET MVC 的视图中

Image is not shown on the view Using ASP.NET MVC

我试图在 json 反序列化 web.Every 数据后显示我的数据,但图像除外。它在网络上仅显示图像符号,但现在我反序列化 Json 对象的控制器 class 的实际 images.My 代码如下 -

public ActionResult PlaceInformation(City objCityModel)
{
    string name = objCityModel.Name;
    ViewBag.Title = name;
    var ReadJson = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/" + name + ".json"));
    RootObject json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<RootObject>(ReadJson);
    List<Poi> mycities = new List<Poi>();

    foreach (var item in json.poi)
    {
        Poi obj = new Poi()
        {
            Name = item.Name,
            Shorttext = item.Shorttext,
            GeoCoordinates = item.GeoCoordinates,
            Images = item.Images,

        };
        mycities.Add(obj);
    }

    ViewBag.Cities = mycities;
    return View();
}

现在在视图中 class 我想显示此 json.But 中的每个项目我在代码旁边提到的图像有错误。错误是:

An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: Unexpected "foreach" keyword after "@" character. Once inside code, you do not need to prefix constructs like "foreach" with "@"

我的密码是-

@model  CallListService.Models.City
@{
    Layout = null;
}

@{
    ViewBag.Title1 = "Show Data";
}

 @foreach (var item in ViewBag.Cities)
 {
     <h2>@item.Name</h2>
     <p>@item.Shorttext</p>

     <p>@item.GeoCoordinates.Longitude</p>
     <p>@item.GeoCoordinates.Latitude</p>

    @foreach (var image in item.Images) //Parser Error
    {
        <img src=@image >
    }
}

同样,如果我只用这种方式写,它可以工作但不显示图像,只显示 img 标志。

<img src=@item.Images>

我没有为此找到任何解决方案。

This is screenshot of web page

源代码:

  <!DOCTYPE html>

  <html>
  <head>
  <meta name="viewport" content="width=device-width" />
  <title>PlaceInformation</title>
  </head>
  <body>
  <div> 

         <h2>Nordertor</h2>
         <p>The Nordertor is an old town gate in Flensburg, Germany, which was built around 1595. Today the landmark is used as a symbol for Flensburg.</p>
         <p>9.43004861</p>
         <p>54.79541778</p>
            <img src="System.Collections.Generic.List`1[System.String]" />
         <h2>Naval Academy M�rwik</h2>

         <p>9.45944444</p>
         <p>54.815</p>
            <img src="System.Collections.Generic.List`1[System.String]" />
         <h2>Flensburg Firth</h2>


         </p>
         <p>9.42901993</p>
         <p>54.7959404</p>
            <img src="System.Collections.Generic.List`1[System.String]" />


   </div>
   </body>
   </html>

我正在使用以下 json 文件来获取所有数据

  {
   "poi":[
    {
      "Name": "Nordertor",
      "Shorttext": "The Nordertor is an old tows used as a symbol for Flensburg.",
      "GeoCoordinates": {
        "Longitude": 9.43004861,
        "Latitude": 54.79541778
      },
      "Images": [
        "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Nordertor_im_Schnee_%28Flensburg%2C_Januar_2014%29.JPG/266px-Nordertor_im_Schnee_%28Flensburg%2C_Januar_2014%29.JPG"
      ]
    },
    {
      "Name": "Naval Academy Mürwik",
      "Shorttext": "The Naval Academy Mürwik is the main training e..",
      "GeoCoordinates": {
        "Longitude": 9.45944444,
        "Latitude": 54.815
      },
      "Images": [
        "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/MSM-hauptgebaeude.jpg/400px-MSM-hauptgebaeude.jpg"
      ]
    },

    {
      "Name": "Flensburg Firth",
      "Shorttext": "Flensburg Firth or Flensborg Fjordg and the villages Munkbrarup, Langballig, Westerholz, Quern, Steinberg, Niesgrau, Gelting, and Nieby.\n\n",
      "GeoCoordinates": {
        "Longitude": 9.42901993,
        "Latitude": 54.7959404
      },
      "Images": [
        "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Flensborg_Fjord_ved_bockholmwik.jpg/400px-Flensborg_Fjord_ved_bockholmwik.jpg"
      ]
    }



    ]


}

图像标签将始终期望该图像的路径

<img src="./images/name.jpg" />

<img src="http:/wwww.sitename.com/images/name.jpg" />

你的图片包含什么图片路径或图片字节?

如果它包含图像字节,则必须通过创建中间页面单独呈现它

将内循环更改为:

foreach (var image in item.Images) //without the `@`
{
    <img src="@image" />  //with " and />
}

如果指定的文件存在,这应该可以工作。 (如果它不起作用,显示完整的错误 + html :-)