Tags

, , , ,

Originally after creating the basic MVC 5 project, the message body was displayed in a numeric format, and the reason was the Entity Framework model was pulling the message from the Service Broker queue as binary-typed data. Fixing this was a simple matter of changing the model item’s data type to string (important: build the project after making this change).

model-item-properties

Now the message body appeared in the view, but as a huge string punctuated with ‘>&amp‘ and suchlike. I was hoping that fixing this was a matter of changing the @Html helper used in the CSHTML file, but I kept getting the same error trying to use Html.Raw() instead of Html.DisplayFor().

cannot-convert-lambda-expr-error

I think this happened because the view was displaying the contents of a single record, whereas the Index view iterates through all records discovered by Entity Framework. You could probably sort out the formatting in the model or controller layer before the data reaches the view, but why not use JavaScript?

Two problems that seem to be happening in MVC 5. First, the str.replace() doesn’t appear to work with data pulled straight from the model. It must be converted to a string beforehand. Second, it doesn’t seen to work if that string is the only variable declared.

multiple-string-variables

As we can see, declaring the Razor helper as a JavaScript variable also enables us to use @Html.Raw() in the Details view. It’s possible to do this with less code:

replace-string-with-pattern

The output in the view should be the XML markup in clear tags.

Advertisements