how to access liferay menu from portlet - gwt

I define a menu in navigation.vm file which working good in liferay project.
But I want to access this menu from my portlet.
Is there any way to access menu from portlet entry point or view.jsp????

import liferay-ui taglib:
<%# taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
then you can use
<liferay-ui:navigation displayStyle="from-level-0" >
</liferay-ui:navigation>
Note: setting displayStyle="from-level-0" to give you the normal behavior like on navigation.vm, you can play with attributes differently to get other behavior.

This link describes the way of getting menu items in a jsp directly.
The below code is reproduced directly from the above link with some improved formatting:
<%# taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%# page import="java.util.List" %>
<%# page import="java.util.ArrayList" %>
<%# page import="com.liferay.portal.model.Layout"%>
<%# page import="com.liferay.portal.kernel.util.WebKeys"%>
<%# page import="com.liferay.portal.theme.NavItem" %>
<%# page import="com.liferay.portal.theme.RequestVars" %>
<%# page import="com.liferay.portal.theme.ThemeDisplay"%>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<div style="width:100%">
<%
//ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String title = themeDisplay.getLayout().getName(themeDisplay.getLocale());
List<NavItem> navItems = new ArrayList<NavItem>();
if (layout != null) {
RequestVars requestVars = new RequestVars(request, themeDisplay, layout.getAncestorPlid(), layout.getAncestorLayoutId());
navItems = NavItem.fromLayouts(requestVars, layouts);
}
for (NavItem navItem : navItems) {
if (navItem.getName().equalsIgnoreCase(title)) {
if (navItem.hasChildren()) {
for(NavItem navChild : navItem.getChildren()) {
%>
<div style="float:left;" class="newsMenuPortlet">
<a href="<%= navChild.getURL() %>" <%=navChild.getTarget() %>>
<%= navChild.getName() %>
</a>
</div>
<%
} // inner for-loop ends here
}
}
}// outer for-loop ends here
%>
</div>

Related

How do I change the default value of a raddropdownlist from outside the control?

I have a raddropdown list and need different default values for when it is used in different pages. This is the code for the raddropdown in th user control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UploadExcel.ascx.cs" Inherits="EntityFramework.Controls.UploadExcel" %>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
<div style="height: 50px;"></div>
<telerik:RadDropDownList ID="RadDropDownList1" runat="server" Width="200">
<Items>
<telerik:DropDownListItem Selected="true" Text="STL010_gen_and_load_settlement" />
<telerik:DropDownListItem Visible="false" Text="LeasingSolarEdge" />
</Items>
I have 2 pages which use this user control and need different values to be selected when the page loads. The page code:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="STL010Upload.aspx.cs" Inherits="SunseapEBT.Web.UploadParameter.STL010Upload" %>
<%# Register Src="~/Control/Upload/UploadExcel.ascx" TagPrefix="uc1" TagName="UploadExcel" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<uc1:UploadExcel runat="server" ID="UploadExcel" />
</asp:Content>
Is there any way to change user control values from the page when it loads the control?
Found a way to change it from the user control by getting the url of the page that requested it.
if (Request.Url.PathAndQuery.Contains("STL010"))
{
RadDropDownList1.Enabled = false;
RadDropDownList1.SelectedIndex = 0;
}
else if (Request.Url.PathAndQuery.Contains("Solar"))
{
RadDropDownList1.Enabled = false;
RadDropDownList1.SelectedIndex = 1;
}

How to access my model from my view?

I cannot seem to figure out how to access my Model from my View. I am confused.
Here is my Home controller. I have verified that "specimens" is being populated with data from the database:
public class HomeController : Controller
{
wildtropEntities wildlifeDB = new wildtropEntities();
public ActionResult Index()
{
ViewData["CurrentDate"] = System.DateTime.Now.ToString("MM/dd/yyyy");
var specimens = from s in wildlifeDB.specimen1
select s;
return View(specimens);
}
}
And here are couple snippets from my View:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<% foreach (WildlifeTropical.Models.specimen s in ??????)
{ %>
<div>s.Name</div>
<% } %>
I assumed I would be able to access "specimens" since I passed it to the View from the Controller (ie, return View(specimens))...but it isn't working.
You will have to make your view strongly typed to a collection of specimen which is what you are passing to it from your controller action (IEnumerable<specimen>):
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<WildlifeTropical.Models.specimen>>" %>
<% foreach (WildlifeTropical.Models.specimen s Model) { %>
<div><%= Html.Encode(s.Name) %></div>
<% } %>
Notice how the view inherits System.Web.Mvc.ViewPage<IEnumerable<WildlifeTropical.Models.specimen>> and it is now strongly typed to a collection of specimens that you will be able to loop through.
This being said, personally I don't like writing foreach loops in my views. They make them look ugly. In this case I would use a display template:
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<WildlifeTropical.Models.specimen>>" %>
<%= Html.DisplayForModel() %>
and then I would define a display template which will automatically be rendered for each element of the model collection (~/Views/Shared/DisplayTemplates/specimen.ascx):
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<WildlifeTropical.Models.specimen>" %>
<div>
<%= Html.DisplayFor(x => x.Name) %>
</div>
See how the specimen.ascx user control is now strongly typed to System.Web.Mvc.ViewUserControl<WildlifeTropical.Models.specimen>. This is because it will be rendered for each specimen of the main view model.
I am not sure about MVC2. In MVC 3(with Razor View Engine), I can pass the Model / ViewModel to the View like this.
#model MyProject.ViewModel.UserViewModel
#{
ViewBag.Title = "Welcome To My Site";
}
<div class="divSubHead">
<h2>Hello #Model.FirstName</h2></div>
How about this? Note the Inherits for the Page.
<%# Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable(Of Specimen))" %>
<% foreach (WildlifeTropical.Models.specimen s in Model)
{ %>
<div>s.Name</div>
<% } %>

ASP.NET MVC2 TeplatedHelper doesn't render an ID of the HTML's markup

I have the code (snippet):
The Model is the IEnumerable object of the Person's class:
<% foreach (var item in Model)
{ %>
<tr>
<td><%= Html.DisplayFor(x=>item.Name) %></td>
</tr>
<% } %>
it renders only labels like that:
<td>Tommy</td>
According to the link it should be rendering a HTML markup something like:
but there is no the ID and the NAME property. Why ?
Your using the wrong template your should be using Html.EditorFor(x => x.Name)
Edit: I said you were using the wrong template because in your image it is a textbox displayed, not a label...
the default ouptut of Displayfor is
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>
according to Brad Wilson. You could easily build your own, look the other post of Brad Wilson for examples.
Or you could simply call Html.LabelFor(x => x.Name)
If you always want that, add a template, name String.ascx in your Views/Share/DisplayTemplate and just put the following in :
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Label("", ViewData.TemplateInfo.FormattedModelValue) %>

Too many JavaScript and CSS files on my ASP.NET MVC 2 Master Page?

I'm using an EditorTemplate DateTime.ascx in my ASP.NET MVC 2 project.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%: Html.TextBox(String.Empty, Model.ToString("M/dd/yyyy h:mm tt")) %>
<script type="text/javascript">
$(function () {
$('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty) %>').AnyTime_picker({
format: "%c/%d/%Y %l:%i %p"
});
});
</script>
This uses the Any+Timeā„¢ JavaScript library for jQuery by Andrew M. Andrews III.
I've added those library files (anytimec.js and anytimec.css) to the <head> section of my master page.
Rather than include these JavaScript and Cascading Style Sheet files on every page of my web site, how can I instead include the .js and .css files only on pages that need them--pages that edit a DateTime type value?
First idea that comes to mind =>
Template:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%: Html.TextBox(String.Empty, Model.ToString("M/dd/yyyy h:mm tt")) %>
<script type="text/javascript">
$(function () {
MakeSureAnyTimeIsIncluded();
$('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty) %>').AnyTime_picker({
format: "%c/%d/%Y %l:%i %p"
});
});
</script>
masterpage or shared external JS file:
function MakeSureAnyTimeIsIncluded(){
if (!anyTimeIsIncluded)
//document.write(<script src="correct url") something like that
anyTimeIsIncluded=true;
}
In your master:
<asp:ContentPlaceHolder ID="Scripts" runat="server" />
And in the Views (aspx) that will use the EditorTemplate/plugin:
<asp:Content ID="indexScripts" ContentPlaceHolderID="Scripts" runat="server">
<script type="text/javascript" src="anytime.js"></script>
</asp:Content>

How to Add, Edit and Display one to many relationship entities in ASP.Net MVC 2?

I am looking for best practices conforming to the MVC design pattern.
My Entities have the following relationship.
tblPortal PortalId PrortalName
tblPortalAlias AliasId PortalId HttpAlias
Each Portal can have many PortalAlias.
I want to Add a New Portal and then Add the associated PortalAlias.
I am confused on how I should structure the Views and how I should present the Views to the user. I am looking for some sample code on how to accomplish this.
My thoughts are first present the Portal View, let the user add the Portal. Then click the Edit link on the Portal List View and on the Portal Edit View let them Add the PortalAlias.
If so, what should the Edit View look like?
So far I have:
Edit View
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.PortalFormViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<% Html.RenderPartial("PortalForm", Model); %>
<div>
<%= Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
PortalForm
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyProject.Mvc.Models.PortalFormViewModel>" %>
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.Portal.PortalId) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Portal.PortalId) %>
<%= Html.ValidationMessageFor(model => model.Portal.PortalId) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.Portal.PortalName) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Portal.PortalName) %>
<%= Html.ValidationMessageFor(model => model.Portal.PortalName) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
Alias<br /><%-- This display is for debug --%>
<% foreach (var item in Model.PortalAlias) { %>
<%= item.HTTPAlias %><br />
<% } %>
PortalFormViewModel
public class PortalFormViewModel
{
public Portal Portal { get; private set; }
public IEnumerable<PortalAlias> PortalAlias { get; private set; }
public PortalFormViewModel()
{
Portal = new Portal();
}
public PortalFormViewModel(Portal portal)
{
Portal = portal;
PortalAlias = portal.PortalAlias;
}
}
Hopefully you've found an answer to this elsewhere, although based on how difficult it is to find information about this online, it's probably unlikely ...
An MSDN blog linked over to ASP.NET MVC, Entity Framework, Modifying One-to-Many and Many-to-Many Relationships (there's a link to the previous in the series in the first paragraph).
But Editing a variable length list, ASP.NET MVC 2-style seems a little better (and includes sample code).