Umbraco ver. 4.7
My project is running along side asp.net MVC 4. I have a Umbraco page, which has a Rich Text editor property on the document type, that will retain a body of text with HTML.
Is there a way to use a System.Net.WebClient client, and request the Umbraco page from within my MVC project and have it return the content of that page's document property to the MVC controller's WebClient request?
Bonus Question:
Is there a way to setup an Umbraco macro in the template that will parse the querystring params and have place holders in the Umbraco template that the macro will replace the placeholders with the parsed out querystring params before the content is returned to the request (explained up above)?
Have you used the /Base extension in Umbraco? I believe this may help you out, because I had similar requirements and found /Base extremely powerful. In my document type I actually used a multi-line textbox as input for the html (or in my case Razor script). In my /Base class I created a method that finds the correct node, takes the raw script from the textbox, renders the script into a raw html string, and returns the string wrapped in a tag .
public static string GetRazor()
{
var doc = new DynamicNode(nodeId);
string razorScript = doc.GetPropertyValue("{propertyAlias}");
string razorString = RenderRazor.RenderRazorScriptString(razorScript, nodeId);
return razorString;
}
Another way is to create a macro within Umbraco which your template can call, something like:
#using umbraco.MacroEngines
#{
dynamic node = #Model;
string htmlString = node.GetPropertyValue("{propertyAlias}");
#Html.Raw(htmlString)
}
Then inside your template, you just need to inject the macro:
<umbraco:Macro Alias="{RazorAlias}" runat="server"></umbraco:Macro>
Whatever html in your textbox will be rendered by the razor engine!
-Dan
Add a reference to umbraco.dll and umbraco.MacroEngines.dll in your MVC4 project.
You should then be able to do (within your controller or view):
dynamic d = new DynamicNode(nodeId);
return d.AliasOfRichTextProperty
Related
I have a need of "showing" a word document in a jsp file, and when a user clicks on the file, it should get downloaded.
It is basically a Spring MVC application, the controller creates a word document and then returns the next view to be displayed to the client. This view file (jsp file) should have the word document.
The controller essentially just returns the view name:
#Controller
String handleRequest() {
// Logic for creating a word document.
return "nextView";
}
It is in this nextView.jsp the attachment should appear.
Now, how can we "attach" the generated word document into the jsp file?
Any thoughts in this?
Using an iframe with in the page, you can invoke one more requestMapping URI which has header content-type set to excel / word doc and that document can be displayed on fly. And after the iframe add a download link as shown in the below link.
Ref here : http://www.codejava.net/frameworks/spring/spring-mvc-with-excel-view-example-apache-poi-and-jexcelapi
How do I make a mailto link using Razor?
I've seen Html.MailTo, but when I try #Html.MailTo nothing comes up.
Thanks!
You should just make a normal hyperlink:
...
Razor and MVC helper methods are not intended to replace HTML tags; they're intended to make common data-bound elements simpler.
HTML.MailTo() helper is a part of the 'mvc3 futures' project, but there is an alternative to way to do it.
1.)Create a new .cshtml file inside App_Code directory and name it as you want (for example HTMLHelpers.cshtml)
2.)Write the following in the file
#helper EmailTextBox(string email, string title) {
#title
}
3.)Now in your view you can call your new function. For example write
Email: #HTMLHelpers.EmailTextBox("george#example.com","George Chatzimanolis")
I get to this question when I was trying to do simple
Email
but Razor will treat this as text
I know there are HtmlHelpers and such but so far simple code worked for me
Email
Whether a variable comes from model or ViewBag it's about # sign in href and Razor
The mailto helper is a part of the 'mvc3 futures' project.
The below blog will give you more information on mvc3 futures as well as the link to get it. I believe that it is also available as a NuGet package.
http://weblogs.asp.net/imranbaloch/archive/2011/07/26/using-the-features-of-asp-net-mvc-3-futures.aspx
#{var email= new HtmlString( "" mail me ""));}
#email;
Just starting to get the hang of MVC3 and want to start building out some custom controls that I can just drop into a view. Specifically I want to be able to drop in some html form controls that will automatically add some javascript for validation.
something like this is what I'd want:
#Html.VTextBox("MyTextBox","",new {#vType="PhoneNumber", #vMessage="You Must Enter a Phone Number" })
when the page is rendered, I'd want it to search for any custom elements I create then drop in the appropriate javascript to validate client side. I did something very similar to this with asp.net web forms for custom controls ie
<ucc:VTextBox ID="MyTextBox" runat="server" message="You must fill this in" />
just wondering if MVC3 offers the same extensibility. Any resources or advice you can provide would be greatly appreciated.
TIA
To start off with, you'll need to look into extension methods and more specifically, how to create extension methods for the HtmlHelper class so you could write code like the example you've shown above.
For the example you've shown above, you could write code like this:
public MvcHtmlString VTextBox(string name, object value, object htmlAttributes)
{
StringBuilder html = new StringBuilder();
// Build your html however you want
// Here's a simple example that doesn't
// take into account your validation needs
html.AppendFormat("input type=\"text\" name=\"{0}\" value=\"{1}\" />", name, value);
return MvcHtmlString(html.ToString());
}
Then in your view you can use the example above like so:
#Html.VTextBox("MyTextBox","",new {#vType="PhoneNumber", #vMessage="You Must Enter a Phone Number" })
NOTE: You'll need to import the namespace the extension method is in to your view. Simplest method, put a #using ExtensionMethodNamespace at the top of your view. You can make the namespace automatically imported to all your views by fiddling with the web.config (and maybe the Global.asax).
ADDENDUM: Please note RPM1984's comment below where he advises to use TagBuilder in place of StringBuilder which is sound advice since this is the exact scenario TagBuilder was designed for. He also mentions strong typing the helper to the model which is also great advice.
What is the best way to output an MVC page as PDF and switching the master page to a different master page?
I was thinking I could add a parameter to the query string like format=pdf
and set some sort of filtering that capture the output and transform it to a pdf.
Using classic ASP.NET I did that in an HttpModule.
What is the best way of doing it in MVC?
I have to be able to "print" all the pages in my application, so a FileResult controller method would not work.
I need something generic that can work with every url adding that specific query string parameter
Write a FileResult controller method.
How to create file and return it via FileResult in ASP.NET MVC?
As part of the return result you set the MIME type.
I have a table in my db where one of the properties is an Html page (without the html, head and body tags), and I intend to put it in the middle of one of my views - say, I call a cotroller method that takes an argument, and return a view passing this html big string as the model. I searched for it (not much, I admit), and found the following method:
<%= System.Web.HttpUtility.HtmlDecode(yourEncodedHtmlFromYouDatabase) %>
That was found here in stackoverflow. When I tried a similar razor aproach, I ended up with this:
#System.Web.HttpUtility.HtmlDecode("<h1>Test</h1>")
That's the idea, but it didn't work quite as I planned.
All you need is: #Html.Raw(yourEncodedHtmlFromYouDatabase)
I'm assuming that the html in the database has been properly sanitized (or at least from a reliable source), because if not, you could be opening yourself up to cross-site scripting attacks.
The reason your approach didn't work is that Razor HTML-encodes output by default (every time you use # to display something). Html.Raw tells Razor that you trust the HTML and you want to display it without encoding it (as it's already raw HTML).
You can also return a HTMLString and Razor will output the correct formatting, for example.
#Html.GetSomeHtml()
public static HtmlString GetSomeHtml()
{
var Data = "abc<br/>123";
return new HtmlString(Data);
}
This will allow you to display HTML