var results = JsonConvert.DeserializeObject<List<MyData>>(result);
imageSrc = new List<string>(); foreach (var item in results) { //output the actual item //Console.WriteLine(Filename); // Page.Response.Write("script>console.log('" + Filename+ "');</script>");
imageSrc = new List<string>();
foreach (var item in results)
{
imageSrc.Add("~/Content/UploadedImages/" + item.Filename);
In the View you are trying to render the List<string>
<span>@{ @imageSrc }</span>
As recommended in your similar thread either use an index or a loop.
IMHO, it seems you have general design issues but you have not posted enough code to understand what you are trying to do. Please post enough code to reproduce this issue. Use the "Insert/Edit code samples" when posting your code on the forum. The toolbar
item looks like {;}. Last;y, post complete code samples so the community does not have to guess.
Member
8 Points
27 Posts
Convert JSON data to list and get filename not working as intended.
Nov 06, 2019 02:33 PM|AltFire|LINK
Hi,
Here is code :
public class MyData
{
public string Filename { get; set; }
public string OriginalName { get; set; }}
The JSON is like this:
[{"Filename":"ModuleName/00002/00002041_nzc3gazlsjayg.jpg","OriginalName":"kevin-mueller-ykrpdbqFMQM-unsplash.jpg"},{"Filename":"ModuleName/00002/00002041_7yc3pbvdiqxdm.jpg","OriginalName":"jason-leung-KFMYmlGUmcM-unsplash.jpg"}]
var result = Model.Modulename.Image;
var results = JsonConvert.DeserializeObject<List<MyData>>(result);
imageSrc = new List<string>();
foreach (var item in results)
{
//output the actual item
//Console.WriteLine(Filename);
// Page.Response.Write("script>console.log('" + Filename+ "');</script>");
imageSrc.Add("~/Content/UploadedImages/" + item.Filename);
<tr>
<td>
<span>@{ @imageSrc }</span>
<img src=imageSrc alt="images" style="width:128px;height:96px;" />
Results in Span:
System.Collections.Generic.List`1[System.String] type name not actual value, even though iterating through collection.
Results in img: Empty box.
I think what is happenning is the JSON deserialising is working to the get the right number of "results" from "result".
So it has a list of length corresponding to the number of pictures.
But how to get the results, split by Filename, how to see if the right result is going into filename?
Switched web.comfig to use debug, f12 can't see variable values in console.
Regards
All-Star
53041 Points
23619 Posts
Re: Convert JSON data to list and get filename not working as intended.
Nov 06, 2019 02:58 PM|mgebhard|LINK
You still have not fixed the problem point out in your duplicate thread!
https://forums.asp.net/t/2161324.aspx?See+values+at+run+time
You clearly defined imageSrc as a List<string>.
imageSrc = new List<string>(); foreach (var item in results) { imageSrc.Add("~/Content/UploadedImages/" + item.Filename);
In the View you are trying to render the List<string>
As recommended in your similar thread either use an index or a loop.
IMHO, it seems you have general design issues but you have not posted enough code to understand what you are trying to do. Please post enough code to reproduce this issue. Use the "Insert/Edit code samples" when posting your code on the forum. The toolbar item looks like {;}. Last;y, post complete code samples so the community does not have to guess.
Member
8 Points
27 Posts
Re: Convert JSON data to list and get filename not working as intended.
Nov 06, 2019 03:04 PM|AltFire|LINK
<span>@{ @item.Filename }</span>
Prints out:
ModuleName/00002/00002029_6zc2ejs7br366.png
So concate should have yielded full path
~/Content/UploadedImages/ModuleName/00002/00002029_6zc2ejs7br366.png
which works in a static position in same form. So something wrong with
imageSrc.Add("~/Content/UploadedImages/" + item.Filename);
or
<img src=imageSrc alt="images" style="width:128px;height:96px;" />
Member
8 Points
27 Posts
Re: Convert JSON data to list and get filename not working as intended.
Nov 06, 2019 03:27 PM|AltFire|LINK
So I did:
var isrc=("~/Content/UploadedImages/" + item.Filename);
<span>@{ @isrc}</span>
which gives full path:
~/Content/UploadedImages/ModuleName/00002/00002029_qscavrdrzxa5c.jpg
Copying this and pasting works in static position on same form.
But image still is empty when doing this;
<img src=isrc alt="images" style="width:128px;height:96px;" />
All-Star
53041 Points
23619 Posts
Re: Convert JSON data to list and get filename not working as intended.
Nov 06, 2019 03:45 PM|mgebhard|LINK
The Razor syntax is.
<img src="@isrc" alt="images" style="width:128px;height:96px;" />
I can not provide an accurate solution without all the relevant code. I'm guessing you'll still get a 404 due to the "~".
Member
8 Points
27 Posts
Re: Convert JSON data to list and get filename not working as intended.
Nov 06, 2019 03:56 PM|AltFire|LINK
Yeah, you were right, the tilde, tried it without working as expected.
You are a legend!
Thanks.