locked
Convert JSON data to list and get filename not working as intended. RRS feed

  • Question

  • User416875641 posted

    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

    Wednesday, November 6, 2019 2:33 PM

Answers

  • User475983607 posted

    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 "~".  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 3:45 PM

All replies

  • User475983607 posted

    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>

    <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.

    Wednesday, November 6, 2019 2:58 PM
  • User416875641 posted

    <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;" />

    Wednesday, November 6, 2019 3:04 PM
  • User416875641 posted

    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;" />

    Wednesday, November 6, 2019 3:27 PM
  • User475983607 posted

    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 "~".  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 3:45 PM
  • User416875641 posted

    Yeah, you were right, the tilde, tried it without working as expected.

    You are a legend!

    Thanks.

    Wednesday, November 6, 2019 3:56 PM