Pretty new to ASP.NET and trying to figure out how to have the code just echo the information it fetched.
<ul class="accordion">
<% foreach (var cat in Model.Categories.Where(x=>x.ParentId==null).OrderBy(x=>x.DisplayOrder).ThenBy(x=>x.CategoryName)) { %>
<li style="height: auto; min-height:30px;">
<%= Html.ActionLink(cat.CategoryName, "Index", "Products", new { id=cat.UniqueName, area="" }, null)%>
<%if(cat.SubCategories.Count>0) {%>
<ul class="sub-menu" style="font-size:.8em; padding-left:20px;">
<% foreach (var c in cat.SubCategories) { %>
<li><%= Html.ActionLink(c.CategoryName, "Index", "Products", new { id=c.UniqueName, area="" }, null)%></li>
<% } %>
</ul>
<%}%>
</li>
<%} %>
</ul>
Currently I know it has to do with <%= Html.ActionLink(cat.CategoryName, "Index", "Products", new { id=cat.UniqueName, area="" }, null)%> that line but not sure how to make it pass the information. Might be a stupid question but Google definitely did not help when searching for this answer.
Thanks.
Here is a brief overview of some of the commands:
<%= Html.ActionLink(cat.CategoryName, "Index", "Products", new { id=cat.UniqueName, area="" }, null)%>
Will create:
{CategoryName}
Which can also be constructued as:
<%: cat.CategoryName%>
Which will also create:
{CategoryName}
So, you can output just the name or ID using:
<%: cat.CategoryName %>
OR
<%= cat.CategoryName %>
<%= is "Response.Write" and "<%:" Is Response.Write with HTML Encode
Related
I have a navbar that is a partial view that I need to render on a devise page for the user to edit their profile. As it is, I only have one page, but adding the path to perform account maintenance has messed up my navbar loading because of the instance variable not being present. How can I get a global instance variable that works across the board for my navbar, no matter what?
application_helper.rb
def get_company
#company = current_user.company
end
def get_locations(company)
#locations = if #company
current_user.company_locations.order(:name)
else
[]
end
end
pages_controller.rb
def home
#company = get_company
#locations = get_locations(#company)
#reports = if #company
#company.reports.order("created_at DESC").limit(5)
else
[]
end
end
views/devise/registrations/edit.html.erb
<%= render partial: "pages/navigation" %>
... devise form ....
pages/_navigation.html.erb
<li class="right-menu-item">
<button class="btn dropdown-toggle " type="button" id="dropdownMenu1" data-toggle="dropdown">
<% if #company %>
<%= #company.name %>
<% else %>
<%= current_user.email %>
<% end %>
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu1">
<% #locations.each do |location| %>
<li role="presentation">
<%= link_to root_path(location_id: location.id), role: "menuitem", tabindex: "-1" do %>
<%= image_tag "check-green.svg" if location == #location %>
<span><%= location.name %></span>
<% end %>
</li>
<% end %>
</ul>
Where the issue lies:
What am I missing here to get this working across all views?
Thanks!
Ok, seems that Where is the controller of Devise ? how to add data from other models in users/edit page? was helpful. I had to do the following to make this work in the partial for the devise views:
rails g devise:controllers users -c registrations
which made a registrations controller under controllers/users/registrations.rb, then i was able to add the instance variables I needed do the edit method:
def edit
#company = get_company
#locations = get_locations(#company)
end
Had to also change the route.rb file around a little to facilitate the new controller:
devise_for :users, skip: [:sessions],
controllers: {
registrations: 'users/registrations'
}
Hi I'm using silverstripe 2.4.7 and I'm having difficulty getting the pagination to work. I've created a function in my page.php to get the latest articles like so
function AllNewsPosts($num=1) {
$news = DataObject::get_one("NewsHolder");
return ($news) ? DataObject::get("NewsEntry", "ParentID > 0", "Date DESC", "", $num) : false;
}
Then when i put this function into the control and pagination tags one article shows up however the links to the concurrent articles do not work - essentially the pagination is not working and I'm not sure how to fix it
<% if AllNewsPosts %>
<% control AllNewsPosts %>
<div class="event">
<h2>$MenuTitle |<span class="date"> $Date.Time $Date.Long</span></h2>
<p>$Content.FirstParagraph</p>
See more about this event
</div>
<% end_control %>
<% else %>
<div class="no-entry">'There are no entries'</div>
<% end_if %>
<% if AllNewsPosts.MoreThanOnePage %>
<div id="PageNumbers">
<p>
<% if AllNewsPosts.NotFirstPage %>
<a class="prev" href="$AllNewsPosts.PrevLink" title="View the previous page"><span class="yellow-background">Prev</span></a>
<% end_if %>
<span>
<% control AllNewsPosts.PaginationSummary(0) %>
<% if CurrentBool %>
<span class="current">$PageNum</span>
<% else %>
<% if Link %>
$PageNum
<% else %>
…
<% end_if %>
<% end_if %>
<% end_control %>
</span>
<% if AllNewsPosts.NotLastPage %>
<a class="next" href="$AllNewsPosts.NextLink" title="View the next page"><span class="yellow-background">Next</span></a>
<% end_if %>
</p>
</div>
<% end_if %>
Any help is much appreciated
Note: The following answer is for Silverstripe 2.4. This should not be used for Silverstripe 3.0+ sites. From 3.0 and onwards the PaginatedList object makes pagination much easier.
You are not setting a limit on how many entries to retrieve in your query, or where to start from.
The following tutorial explains how to apply pagination to a set of data objects exactly as you are trying to do:
http://www.ssbits.com/tutorials/2010/paginating-a-filtered-dataobjectset/
Here is an attempt at altering your function to include limit and start as needed for pagination:
PHP
function AllNewsPosts() {
if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1)
{
$_GET['start'] = 0;
}
$SQL_start = (int)$_GET['start'];
$newsEntries = DataObject::get('NewsEntry', '', 'Date DESC');
$doSet = new DataObjectSet();
foreach ($newsEntries as $newsEntry) {
if ($newsEntry->canView()) {
$doSet->push($newsEntry);
}
}
$doSet->setPageLimits($SQL_start, 10, $doSet->Count());
return $doSet;
}
Note the above will display 10 items per page. You can change this to however you need per page.
Why does this not work? I've tried multiple choices like render and puts, but it doesn't want to change properly. A couple of ideas users?
THE VIEW:
<div id = "container">
<div id = "smaller-container">
<%= user_signed_in? %>
<%= #Uchecker %>
<% else %>
<%= link_to "Sign Up", new_user_registration_path, :id => "SignUpText" %>
</div>
THE CONTROLLER:
class PostsController < ApplicationController
def Uchecker
#Uchecker = redirect_to user_session_path
end
end
I will like to achieve the following html using Html.ActionLink:
<li>John DoePresident</li>
The name "John Doe" and title "President" will be coming from a staff model. This is what I have:
<% foreach (var item in Model as IEnumerable<AkwiMemorial.Models.Staff>)
{ %>
<li><%= Html.ActionLink(item.Name, "GetStaffDetails", "WhatWeDo", new { staffID = item.Id }, null) %> item.Position</li>
<% } %>
Instead of rendering "item.Position" literally, I will like this string extracted from the model.
TIA.
Try this:
<% foreach (var item in Model as IEnumerable<AkwiMemorial.Models.Staff>)
{ %>
<li>
<%= Html.ActionLink(item.Name, "GetStaffDetails", "WhatWeDo", new { staffID = item.Id }, null) %>
<%=item.Position%>
</li>
<% } %>
View
<%= Ajax.ActionLink("Create", "Create", ViewData.Model, new AjaxOptions { HttpMethod = "POST" })%>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Ajax.BeginForm("Create", "Customer", ViewData.Model, new AjaxOptions { HttpMethod ="POST" }))
{%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Title">Title:</label>
<%= Html.TextBox("Name")%>
<%= Html.ValidationMessage("Name", "*")%>
</p>
<p>
<label for="Description">Description:</label>
<%= Html.TextArea("ContactNo")%>
<%= Html.ValidationMessage("Name", "*")%>
</p>
</fieldset>
<% } %>
Controller
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Customer info)
{
//INFO IS NULL???
//WHAT IS THE PROBLEM?
}
You can't pass the model object. This argument expects the route values such as an ID.
if you pass in Ajax.ActionLink("Create", "Create", new { id=23 }, ....
it will create /create/23.