Inject comment with partial name during RenderPartial - asp.net razor - asp.net-mvc

mvc4, razor viewengine. It would be useful if, when in debug mode, a snippet like this that includes a partial would leave a comment saying what partial it injected. Are there extensibility points that I just can't find? Obviously I could just type out this comment in the partial views, but that sounds like WAAYY too much work and isn't fun for anybody.
index.cshtml
<div class="somecontainer">
#foreach(var thing in Model.Things) {
Html.Partial("ThingPartial",thing)
}
</div>
ThingPartial.cshtml
#model ThingModel
<h1>#Model.Name<h1>
<p>#Model.Description</p>
would generate html like this
<!--razorView:things/index-->
<div class="somecontainer">
<!--partialView:ThingPartial-->
<h1>Name 1<h1>
<p>Description 1</p>
<!--partialView:ThingPartial-->
<h1>Name 2<h1>
<p>Description 2</p>
<!--partialView:ThingPartial-->
<h1>Name 3<h1>
<p>Description 3</p>
</div>
Motivation
I want my testers to be able to use the comments to write more accurate bug reports without my developers / me having to do anything extra!
What I have in my mind is something configurable in global.asax.cs, like so
ViewEngines.Engines.Clear();
RazorViewEngine viewEngineToAdd = new RazorViewEngine();
//I COMPLETELY MADE THIS LINE UP, THESE THINGS DON'T EXIST (but I want them to!
viewEngineToAdd.PreRender += (OutputStream,IView) => OutputStream.Write(#"<!--"+IView.Name+"-->")
ViewEngines.Engines.Add(viewEngineToAdd);

Try this:
<div class="somecontainer">
#foreach(var thing in Model.Things) {
Html.Raw("<!--" + thing + "-->");
Html.Partial("ThingPartial",thing)
}
</div>

Related

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

Escape # in Html.Raw

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 -->

EditorTemplate auto-generated divs

I'm using a editor template for a object called Address.
#Html.EditorFor(model => model.Address)
In the template I have a few text boxes for the information about the user address.
this is how the text boxes are written in template
<p class="clear">
<label for="zip">
<span>#Html.LabelFor(x => x.ZipCode)</span>
#Html.TextBoxFor(m => m.ZipCode, new { #class = "big zip" })
</label>
</p>
but when it's rendered they broke my CSS putting
<div class="editor-label"> AND <div class="editor-field">
in the place of my paragraph tags.
There's any way I could change that?
I don't think your editor template is actually being used. Those are the default template values. You will have to provide some more information about your template in order to help. Where is it located? What is it called? etc.. (update your question, don't add this information as a comment)
What you would normally do is have a file called Address.cshtml and place this in a folder called EditorTemplates, either in the same folder as the view you are referencing, or in the Shared views folder if you need to share this template across various folders.
You should also add an #model Addresss (or whatever the fully qualified namespace is).
BTW, you are two labels being generated. That's not semantically correct.
Although not with Razor syntax, this article from Brad Wilson explains display/editor templates pretty well

Can I simplify this code with an HTML Helper?

I have the following code on my web pages:
<div class="rep_tb0" id="Activity" style="display:none;">
<div class="rep_tr0">
<div class="rep_td0" id="ActivityLog">Activity Log<br /><br /></div>
</div>
</div>
It's repeated for many pages and I would like to just code it once in one place. Can anyone tell me what are my options for doing this. Can I code an HTML Helper or is there a better / another way of doing this?
I think you need to use a partial in this case. Just create a .cshtml file and put that code in. Then call
#Html.Partial("PartialName");
Yes, build an HTML helper by creating an extension method. It's a lot easier than you think.
Check out this article:
http://weblogs.asp.net/jgalloway/archive/2011/03/23/comparing-mvc-3-helpers-using-extension-methods-and-declarative-razor-helper.aspx
Or this article:
http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

How to render partial view in asp.net mvc 2 using Controller and Action?

<body>
<div id="header">
<div class="title">SPORTS STORE</div>
</div>
<div id="categories">
<% Html.RenderAction("Menu", "Nav"); %>
</div>
<div id="content">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</body>
This is a sample code from Steven Sandersons' book "Pro ASP.NET MVC Framework."
This code works with MVC V1 and MvcContrib. What it does, it renders a "Menu()" view of "Nav : Controller". Since ASP.NET MVC V2 includes the Partial() and RenderPartial() functionality, I tried to implement it, but this code doesn't work. I tried to modify it in several ways, but it seems there's no overload function that renders partial views, taking as parameters:
1) Controller name
2) View name
I'm sure I'm not the first person who is implementing RenderAction() in this way, so, there must be a workaround. Please help.
I found the problem. I always remove {controller}/{action} route and customize all my routes with lowercase REST like URLs. But for Html.RenderAction it is necessary to have such general route. I added that general route to the end of my routes list and it worked. – Mahdi Feb 22 at 14:42
Although i still don't understand, how EXACTLY this works, why is suck route nessesary, and what are route's constraints. Maybe i will do some research later.
RenderAction is in MVC2 (docs here). It sounds like you've changed your code to use RenderPartial instead which is completely different. Change it back to use RenderAction and you should be ok. If you don't have it in your version, perhaps you need to update to the latest beta?

Resources