1

I have a controller method that returns image from MongoDB, and I want to show it in my view:

<HttpPost()>
        Function ShowImage(cardNumber As String) As FileContentResult

            Dim Handler = New MongoDBHandler()
            Dim newString = cardNumber.Replace(vbLf, "").Trim().Replace("""", String.Empty)
            Dim byteArray = Handler.ReadImage(newString)

            Return File(byteArray, "image/png")
        End Function

I have the javascript function:

function postCardNumber(elm) {
    var CardNumber = $(elm).closest("tr").find(".card-number").html(); 
    var $img = $('<img>');
    $img.attr('src', "data:image;base64," + @Html.Action("ShowImage", "CreditCard", CardNumber));
    $("#myModal").append($img);
}

But there is a red underline under "CardNumber" parameter for the attr function. Why?

xlm
  • 6,854
  • 14
  • 53
  • 55
Ballon Ura
  • 882
  • 1
  • 13
  • 36

1 Answers1

0

Are you sure that razor templates works with JavaScript? You can translate your razor syntax to js/HTML. But I'm not sure that its works vice-versa. Razor syntax transpiled when your page rendered by the server,js starts working when page loaded. You should rewrite your code to js without using razor in this way

blckt
  • 81
  • 8
  • so what the solution for passing parameters without razor ? – Ballon Ura Jan 22 '17 at 21:14
  • Just render your URL and pass it as a strings,then add query params – blckt Jan 22 '17 at 21:18
  • didnt understand the solution – Ballon Ura Jan 22 '17 at 21:19
  • @Html.Action("ShowImage", "CreditCard") this thing will be rendered to '/CreditCard/ShowImage', third argument are params passed to query,so you should replace your code to plain string and add params. Result will be 'CreditCard/ShowIamge/'+CardNumber – blckt Jan 22 '17 at 21:23
  • You can help me how replace the returned byteArray instead of CardNumber parameter? – Ballon Ura Jan 22 '17 at 21:26
  • Actually, i`m not sure,that you should use "data:image;base64". I think,if you put URL to your endpoint,browser should handle everything else without you. In the other side, if you need serverside rendered images,look at this solution http://stackoverflow.com/questions/17952514/mvc-how-to-display-a-byte-array-image-from-model – blckt Jan 22 '17 at 21:34