I am trying to display content such as
<p>Text and link - #Html.ActionLink("Link", "Action")</p>
from a database, but if I use #Html.Raw then it doesn't render the link.
Is there anyway to do this?
You will need to use a Razor parser in order to achieve that. Checkout RazorEngine which could be used render the Razor markup to HTML.
print it on the backend using this:
string template = "Hello #Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });
pass the results to front end using a model which contains the result in a string property.
you will need to install the following:
Install-Package RazorEngine
Related
I am returning data from my DB with multiple phrases. One of them being the following text : Submitted an Idea
I want to make the "Idea" in any an all text a hyperlink, so I want to use a replace function in my razor view to replace the word "Idea" with my Html Helper:
#item.RewardType.Replace("Idea", #Html.ActionLink("Idea", "ChallengeIdea", "Ideas", new { id = item.fkiIdeaId }, null))
I've looked around a bit but can not really find anything. Someone suggested using #Url.Action - But the issue remains the same.
How do I do this ? Or is using an Html helper the wrong way of doing this ?
Thanks for any help.
You can try this:
#Html.Raw(item.RewardType.Replace("Idea", $"<a href='/ideas/challengeidea/{item.fkiIdeaId}'>Idea</a>"))
Or
#Html.Raw(item.RewardType.Replace("Idea", "Idea"))
Html helpers are there to help you in general situations. When they produce more complications than value, they have no use
<span>Submitted an Idea</span>
If you have RewardType in a resource and can not use plain html, you could set RewardType to "Submitted an Idea" And use string.format
I have a .cshtml file which serves as an order confirmation as html email.
Now I would like to read this .cshtml file and bind a certain viewmodel against the .cshtml file.
In the end I want to recieve a formatted html string to pass to my email message body.
How can I do this in an elegant way even if it needs 3rd party open source thats fine.
You could use the RazorEngine NuGet which allows you to render a Razor template to a string:
string template = "Hello #Model.Name, welcome to RazorEngine!";
var result = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" });
I have used the Grails Rendering Plugin in the past with much success in creating PDFs. Throw now I would like to create a simple text file, using a gsp. I loved the ease of using a model to define how to insert information into the template. I realize I don't need to render a text file, but is there a similar way to use a template to just create an ordinary text file?
Example from how to render a jpg using the Grails Render Plugin: (notice the model use)
def bytes = gifRenderingService.render(template: '/images/coupon', model: [serial: 12345])
// Render to a file
new File("coupon.jpg").withOutputStream { outputStream ->
jpegRenderingService.render(template: '/images/coupon', model: [serial: 12345])
}
If there isn't an easy way like the above example, since my information is coming from multiple domain classes should I just create <g> tags in my gsp template that pulls based on conditions needed? If that is the case.. how would I insert a variable into my gsp template from my service?
You might take a look at the grails.gsp.PageRenderer utility class. It allows you to render .gsp templates as a String:
String gspOutput = groovyPageRenderer.render(view: '<your view>.gsp', model: [ modelObj1: ... ])
... or directly to a Writer or OutputStream:
groovyPageRenderer.renderTo(view: '<your view>.gsp', model: [ modelObj1: ... ], <writer or OS>)
Much more detail can be found here: http://mrhaki.blogspot.com/2012/03/grails-goodness-render-gsp-views-and.html
To render the template as text file you should set the content type of the response to text/plain
I'd think you just create your text template as standard, with <g> tags etc..., then call the standard grails render() function on the template with a contentType of 'text/plain'. No plugin necessary?
Add the following code to the GSP file
<%#page contentType="text/plain"%>
What's the syntax for displaying this? My program will always render « literally in the pagination helper I am creating, and since the switch to MVC3 there seems to be no way to even hack it with Eval( string.FromCharCode )
Just use the corresponding UTF-8 character and the HTML helper will take care of the encoding it:
#Html.RouteLink("«", new { action = "navigate", page = 1 })
#Html.RouteLink("»", new { action = "navigate", page = 3 })
As long as you are using UTF-8 as your character-set then showing the « directly (without encoding it) is perfectly valid XHTML.
I'm using MVC 3 (the ASPX ViewModel) while I store and display data from my SQL database. I've tried using the raw input to store it as well as using HttpUtility.HtmlEncode. Neither are working when I try to display. I've tried using the HttpUtility.HtmlDecode as well as using <%: Model.MyHtmlVariable %>. Am I missing something?
Using the traditional "<%= html %>" syntax should render it out for you but may not depending on what you're doing. If not, try to wrap it in an HtmlString object, like so:
<%= new HtmlString(html) %>
MVC should respect that and render it out properly.
If you're just looking to display the encoded HTML, the "<%: html %>" syntax is your friend
You need to create a div to target and set the html using an jquery/javascript call to the controller action.
jQuery.get("/Controller/Action",
function(response) {
$("#MyDiv").html(response)
});
See if something like that works.