aliyousefian
aliyousefian

Reputation: 449

cant use ajax to render img tag

i used ajax for return current user profile image but it bring this img with true src but doesn't show me the place of image is in my layout if i use this img tag in my html work but it dosent work from request in mvc5

 public async Task<string> GetImageUrl()
    {
        var id = User.Identity.Name.SplitFormAuthentication().id;
        var firstOrDefault =await _user.Where(m => m.Id == id).FirstOrDefaultAsync();
        if (firstOrDefault != null)
        {
            string url =string.Format("<img src='~/File/Profile/{0}' id='' alt='' />", firstOrDefault.Avatar);
           var s=WebUtility.HtmlDecode(url);
            return s;
        }
        return "";
    }

and in layout ajax call is

<script>
        $(document).ready(function () {
            GetImageUrl();
            function GetImageUrl() {

                //alert($(this).parent().parent().parent().attr("class"));

                var pa = $(".addEdu");

                $.ajax({
                    url: "@Url.Action("GetImageUrl","Home")",
                    type: "POST",
                    dataType: "html",
                    context: this,
                    data: {

                    },
                    success: function (data) {
                        alert(data);
                        $("#profileimg").html(data);
                        $(".ssss").html(data);

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("");
                    }
                });

            }
        })
    </script>

Upvotes: 1

Views: 100

Answers (1)

kodikotriftis
kodikotriftis

Reputation: 166

It can not resolve the image url like this.

string url = string.Format("<img src='~/File/Profile/{0}' id='' alt='' />", firstOrDefault.Avatar);

Try

string url = string.Format("<img src='{0}' id='' alt='' />", Url.Content(string.Format("~/File/Profile/{0}", firstOrDefault.Avatar)));

Upvotes: 1

Related Questions