Can I simplify this code with an HTML Helper? - asp.net-mvc

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

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

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>

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

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>

Can Struts 2 handle 3 action class calls at the same time

I am using ajax to load 3 jsps in a master page but the problem is it loads one after the other? I wanted to know if struts 2 can handle 3 action class calls at the same time? Or is it even possible to make such calls? My page is getting slow because of this, is there any other way to handle this?
<div id="content">
<div class="col1">
<div id="content_right">
<div class="curved section_content">
<div id="sub_header" class="curved_top">
<h2>
<s:text name="ft.history.title.details" />
</h2>
</div>
<sj:div cssClass="ajaxDiv" href="transferdetailhistory"
id="transfer_detail_content">
<div class="ajaxSection">
<div class="centeredAjaxSection">
<center>
<img style="vertical-align: middle;" alt="Loading"
src="${pageContext.request.contextPath}/img/bigindicator.gif">
</center>
</div>
</d‌​iv>
</sj:div>
</div>
</div>
</div>
this would load the history page, like wise i load 2 more jsps
Struts2 can handle significantly more than three simultaneous requests. There are many large, scalable applications using the Struts2 framework.
The issue is most likely related to your code.
we are in our application using such cases in every now and than and never faced problem with this.
better come up with some code may be problem with your code or some configuration as Steven said in his repply

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