ADS MB
ADS MB

Reputation: 175

<img> src won't be change using Java Script in [ View ] [ ASP.NET MVC ]

I try to detect if <img> src not found change it with another src using JQUERY, I work on ASP.NET MVC project, and the src come from Controller.

This is what I try :

HTML

@foreach (var item in ViewBag.DATA)
  {

if (item.IMG_FRMTR != null)

     {

      ViewBag.IMG = String.Format("data:image/jpg;base64," + Convert.ToBase64String(item.IMG_FRMTR));

     }

  }

   <img id="IMG" src="@ViewBag.IMG" onerror="standby()" style="width:200px;height:200px">

JQUERY

    function standby() {
       
        document.getElementById('IMG').src = 'https://www.google.com/images/srpr/logo11w.png';

    }

Controller Action

            ViewBag.DATA = PROF_model.Select(CIN);

            return View();

PROF_model.Select Method

    public static List<FRMTR> Select(string CIN)
    {
        var R = (from P in SCHOOL_DB_Context.Con.FRMTRs where P.CIN_FRMTR == CIN select P).ToList();

        return R;
    }

Please any help ?

Note : I look for a lot solution here in Stackoverflow but no one solve my problem

Thanks in advance <3

Upvotes: 0

Views: 89

Answers (1)

ADS MB
ADS MB

Reputation: 175

Thanks for @Pete, he gaves me a great solution that is :

Using just HTML with Tag Helpers like this :

<img id="PROF_IMG" src="@(string.IsNullOrWhiteSpace(ViewBag.IMG)?"https://www.google.com/images/srpr/logo11w.png" : ViewBag.IMG)" style="width:200px;height:200px">

Upvotes: 1

Related Questions