Escape # in Html.Raw - asp.net-mvc

I need to parse some HTML from the server side. So I've used
#Html.Raw(MyFunction("key"))
MyFunction returns me the below value
<!--some HTML -->
<li>
Please click the link here to see the Changes to Your Screens.
</li>
<!--some other HTML -->
I am expecting the anchor tag to be rendered like,
Changes to Your Screens
but it is rendered instead as below.
Changes to Your Screens
I have already tried
##(MySiteSettings...
,
#:(MySiteSettings...
and
#:##(MySiteSettings...
For escaping '#' in Html.Raw, but no luck.

#Html.Raw is doing what it's designed to do: This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML. I don't have visual studio now, so please use this as a sample and update accordingly
Based your code, i would recommend you to use PartialViews
Somewhere in your current view:
#Html.RenderAction("PartialList");
PartialList ActionResult
[OutputCache(Duration = 60, VaryByParam = "None")]
public ActionResult PartialList()
{
...
//form up your dynamic <a> here...
return PartialView(<pass in your string here>);
...
}
PartialView
#Html.Raw(Model)
<li>
Please click the link here to see the Changes to Your Screens.
</li>
Then you might ask, why do you have to go through all this trouble? Take a look at this
Not mentioned in that answer is the OutputCache which you can leverage on easily.

Define your function like this:
#helper MyFunction(string key)
{
// do your code here, also with #expressions
<li>
Please click the link here to see the Changes to Your Screens.
</li>
}

I came up with this solution.
#Html.Raw(string.Format(MyFunction("key"), MySiteSettings.DocumentURL + "Documents/MyPDFFile.pdf"))
And I replaced the content in my DB as below for the above to work.
<!--some HTML -->
<li>
Please click the link here to see the Changes to Your Screens.
</li>
<!--some other HTML -->

Related

Thymeleaf editing th:field before action

I have a simple pagination that looks like
<ul class="pagination justify-content-end" th:field="*{page}">
<li class="page-item page-item disabled">
<a class="page-link" href="action">1</a>
</li>
</ul>
I would get the following behaviour with Thymeleaf. Clicking on the page and before submitting the action, the value of the th:field associated with the pagination should be changed by setting it to the th:value of the selected page. In other words, clicking on the page number should change the value of "*{page}" and then call the method. Is there any way to do that, for example, combining th:onclick with th:action?
Thymeleaf is a rendering engine, as such it can do nothing once the page is served to the end user. At best u can fill variables into javascript when rendering to achieve the desired result.
As for your described functionality the given code is far to limited or faulty to give a suggestion for a javascript solution, for one th:field on a non-input element doesn't do much. Also the shown href is just a simple href that doesnt interact in any way with the encapsulating th:field.

Umbraco 7 - How to allow users to add new content fields on-the-fly?

I'm setting up Umbraco 7.7 for the first time and creating the document types and templates for a page that displays the people that work at our organization (includes their names, photos, and bios).
How do I configure it such that the content manager can add another "person"—effectively a cluster of divs with user-editable images and text—without having to manually add another "person" to the template? Using Partial Views seems like part of the solution, but I'm unclear on how to fit it all together.
My template (simplified) currently looks something to the effect of:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = null;
}
<!doctype html>
<html>
<body>
<div class="person-bio">
<img src="/media/person-01-photo.jpg">
<p>#Umbraco.Field("person-01-name")</p>
<p>#Umbraco.Field("person-01-title")</p>
<p>#Umbraco.Field("person-01-bio")</p>
</div>
<div class="person-bio">
<img src="/media/person-02-photo.jpg">
<p>#Umbraco.Field("person-02-name")</p>
<p>#Umbraco.Field("person-02-title")</p>
<p>#Umbraco.Field("person-02-bio")</p>
</div>
<div class="person-bio">
<img src="/media/person-03-photo.jpg">
<p>#Umbraco.Field("person-03-name")</p>
<p>#Umbraco.Field("person-03-title")</p>
<p>#Umbraco.Field("person-03-bio")</p>
</div>
<!-- etc., etc. -->
</body>
</html>
Thank you! Any help would be much appreciated.
You'll probably will want to use the Nested Content control for this. It allows you to add a list of entities (in your case persons) on a document
More documentation about the nested content control can be found here: https://our.umbraco.com/documentation/getting-started/backoffice/Property-Editors/Built-in-Property-Editors/Nested-Content
So from my understanding you don't need a partial view. If it's that simple and you want to output only the div I see you're repeating, then loop it:
#foreach (var person in whateverYourCollectionIs) {
<div class="person-bio">
<img src="/media/person-01-photo.jpg">
<p>#person.GetPropertyValue<string>("pseudoNameFieldAlias")</p>
<p>#person.GetPropertyValue<string>("pseudoTitleFieldAlias")</p>
<p>#person.GetPropertyValue<string>("pseudoBioFieldAlias")</p>
</div>
}
That loop will create the exact same html for each person, but with the appropriate names, Titles, Bio etc. This is not the actual code you get to use but it hopefully leads you to the correct direction.
This is the documentation that will help

How can I get a field?

I'm trying to create a partial view macro that list all items (blog entries). I can read its name, but not the entry field like it's content:
#foreach (var page in CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc"))
{
<div class="article">
<div class="articletitle">#page.Name</div>
<div class="articlepreview">
#Umbraco.Truncate(#page.Field("pageContent"),100)
Read More..
</div>
</div>
<hr/>
}
All pages are defined as a ContentPage (document type) where I've added Page Content (pageContent), type: Richtext editor as a Tab: Content element.
Do I need to cast the page or something to Contentpage?
What Im trying to do Is to give a 100 char long preview of the content on my main page so the users can read a short excerpt before clicking on the item.
Technically they are Properties of a doctype, not Fields.
So I believe this is what you are looking for:
#page.GetProperty("pageContent").Value
And truncate...
#Library.Truncate(#page.GetProperty("pageContent").Value,100)
However in this context you should be able to simply use…
#page.pageContent
So...
#Library.Truncate(#page.pageContent,100)
…should work!
I think this will give you the text you are looking for (I think your problem was just that extra #):
#Umbraco.Truncate(page.Field("pageContent"),100)
If you are running on umbraco 7+, try this:
#Umbraco.Truncate(page.AsDynamic().pageContent, 100)
Hope this helps!

How can I include markup as linktext in an anchor<a> in MVC?

Using Razor I have a page I want to put an anchor whose linktext includes markup.
for example:
<div>Line1<div>
I tried the following code:
#Html.ActionLink(#Html.Raw("<div>") + "Create New" + #Html.Raw("</div>"), "Create")
but it didn't work.
I know that this might look a bit odd. But is it possible to do that in MVC?
No, it's not possible. However, you can create a new HTML helper method for this. It should be pretty straight forward to do so. If not you can use URL.Action link.
<a href="#Url.Action("Action", "Controller")">
<div> Create New </div>
</a>

Jquery Tab: Can I use Html.ActionLink to fetch server data?

I'm using JQuery UI 1.7/PACKT book. It said on p.63 that the content of a page can be loaded just by using this statement
<li>AJAX Tab</li>
How can I obtain something with Html.ActionLink()
Thanks for helping.
If you are using the jQuery UI Tabs, Html.ActionLinks will work exactly the same way. The key is to ensure that you have your unordered list items within a div tag with an id of "tabs."
Something like this:
<div id="tabs">
<ul>
<li><%= Html.ActionLink("Tab Name 1", "Action1", new { Controller = "ControllerName" })%></li>
<li><%= Html.ActionLink("Tab Name 2", "Action2", new { Controller = "ControllerName" })%></li>
</ul>
</div>
Just include this within your .aspx file that you want the tabs to appear, and make sure to include the jquery and jquery-ui scripts, and it should work for you. You can read about the various Html.ActionLink parameters by following the link. That should help you understand the ActionLink better.
Hope that helps! Please let me know if you need any additional clarification.

Resources