2

I have a view which display information about a store, at the bottom of this view is a partial view which renders particular promotions for that store in question.

On this view I render the logo for the given store using the below snippet

<div class="controls">
     <img src="data:image;base64,@System.Convert.ToBase64String(Model.RetailerImage)" width="80" height="80" />
</div>

Now when I run it the partial view that loads the promotions throws an error

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.

My partial at the bottom of the view

 <fieldset class="well">
       @Html.Action("ListRetailerPromotions", "Promotion", Model)
  </fieldset>

which calls this controller

   [AuthorizeRolesAttribute(RoleType.Retailer, RoleType.Administrator)]
    public ActionResult ListRetailerPromotions(Retailer retailer)
    {
        PromotionViewModel pvm = new PromotionViewModel
        {
            RetailerId = retailer.RetailerID,
            Promotions = UnitOfWork.Promotion.GetPromotionsForRetailer(retailer.RetailerID).ToList()
        };

        return PartialView(pvm);
    }

But when I get the yellow screen of death as some would say the error message is displayed as

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

But if I comment the partial view out all works as exspected.

UPDATE I got it to work by setting the RetailerImage to null after is has been referenced as follows

<div class="control-group">
                <label class="control-label" for="WebAddress">
                    Preview
                </label>
                <div class="controls">
                    <img src="data:image;base64,@System.Convert.ToBase64String(Model.RetailerImage)" width="80" height="80" />
                </div>
            </div>
            {
                Model.RetailerImage = null;

            }
</div>

Which is a revolting solution I have to admit.

1 Answer 1

1

Try using Raw on your image string

@System.Convert.ToBase64String(@Html.Raw(Model.RetailerImage))

If you see the error message, it complains about non-base 64 characters.

This means the framework has HTML encoded the contents of Model.RetailerImage. One way of preventing this default behavior is to use the Raw feature.

Sign up to request clarification or add additional context in comments.

2 Comments

While this may answer the question, adding a brief explanation as to why or how it works would be helpful as well!
No problem, let me add more info :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.