locked
Text with Image and image title RRS feed

  • Question

  • User-587451972 posted

    Hi,

    I need help with respect to show image with text in .chtml . Like:- SubjectName[Tick]

    I am getting data in a @Model.Student.Subject, which have string data in 'Pass[1], Fail[0]'  where [1] =Tick Image with title "", [0]= Cross image with title "". 

    My requirement is to show student subject result with image. I tried to append text with image, but its not helping me out.

    Below sample code is C# used in .chtml.

     //Model.Student.Subject= "Physics[1],Chemistry[0],Maths[1],Biology[1]"
     string[] arrSubject = Model.Student.Subject.Split(new[] { ',' });
     var studentSubjectResult= new System.Text.StringBuilder();
    
     foreach (var item in arrSubject )
     { 
     //if student subject contain[1]
     if (item.Contains("[1]")){
     //then show subject name with tick image and image title "pass"
     var subjectName = item.Split('[')[0];
     studentSubjectResult.Append(subject+ <img src="" title="Pass"> )
     }
     else{
     var subjectName = item.Split('[')[0];
      //then show subject name with tick image and image title "Fail"
      studentSubjectResult.Append(subjectName+ <img src="" title="Fail"> );
     }

    How i can show the result, text with image using C# in .chtml (view page). 

    Thanks in advance.

    Thursday, May 2, 2019 7:50 PM

Answers

  • User1724605321 posted

    Hi archer85 ,

    I assume there is Images folder in your root application and have a picture named "Capture.png" . 

    On server side , you could create the string like :

    var studentSubjectResult = new System.Text.StringBuilder();
    studentSubjectResult.Append("name1" + "<img src = '/Images/Capture.png' title = 'Pass'/>");
    studentSubjectResult.Append("name2" + "<img src = '/Images/Capture.png' title = 'Pass'/>");
    
    ViewBag.content = studentSubjectResult.ToString();

    On client side ,render as :

    @Html.Raw(ViewBag.content)

    Best Regards,

    Nan Yu

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 3, 2019 1:56 AM

All replies

  • User1724605321 posted

    Hi archer85 ,

    I assume there is Images folder in your root application and have a picture named "Capture.png" . 

    On server side , you could create the string like :

    var studentSubjectResult = new System.Text.StringBuilder();
    studentSubjectResult.Append("name1" + "<img src = '/Images/Capture.png' title = 'Pass'/>");
    studentSubjectResult.Append("name2" + "<img src = '/Images/Capture.png' title = 'Pass'/>");
    
    ViewBag.content = studentSubjectResult.ToString();

    On client side ,render as :

    @Html.Raw(ViewBag.content)

    Best Regards,

    Nan Yu

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 3, 2019 1:56 AM
  • User-587451972 posted

    Hi Nan Yu,

    It worked as expected. Thanks for your help.

    Friday, May 3, 2019 11:48 AM