How can I get the total number of items in a SQL result? - linq

I have the following code:
[code]
<h2>Listado General de Carreras</h2>
<% foreach (var item in Model)
{ %>
<p><span class="titulo"><%=Html.ActionLink(item.Nombre, "Details" , new { id = item.ID }) %></span> <sub> Area: <%: item.Area.Nombre%></sub></p>
<% } %>
<p>
<%: Html.ActionLink("Crear Nueva Carrera", "Create") %>
</p>
[/code]
What I want to do is display a header for every Area in my database and under each area show what Careers the Area has.
How can I efficiently do this with MVC2?
I was thinking of using Linq querys and saving them to the ViewData[] but maybe there is another way?

have you tried the .Count property on the collection?

Related

How to reuse a variable obtained from a loop in a subsequent loop in ejs?

I'm currently working on a menu for an hexo.io site using data from a YAML file and EJS. My point is to generate a menu with sections and subsections directly from the data included in the YAML file.
The yaml file in _data/menu.yml
sections:
Section1:
Section2:
section1:
Activate:
url: /activate
First steps:
url: /first-steps
My ejs file
<% for (var section in site.data.menu.sections) { %>
<ul>
<li>
<%= section %>
<% for (var i in site.data.menu.section) { %>
<ul>
<li><%= i %></li>
</ul>
<% } %>
</li>
</ul>
<hr>
<% } %>
My goal is to reuse the section variable in the second loop so that I can get a menu displaying :
Section1
Activate
First steps
Section2
Until now, I couldn't find how to properly insert this variable in the loop conditions. And I do not wish to hard code section names in the second loop.
Thanks for your input.
Found a way to do it unifying everything. it could still be optimised but works for me so I post it in case others have the same issue:
YAML
sections:
section1:
title: Section 1
sub:
Activate: /activate
First steps: /first-steps
section2:
title: Section 2
EJS
<% for (var i in site.data.menu.sections) { %>
<ul>
<li>
<%= site.data.menu.sections[i].title %>
<% for (var j in site.data.menu.sections[i].sub) { %>
<ul>
<li><%= j %></li>
</ul>
<% } %>
</li>
</ul>
<hr>
<% } %>

My Search returns an empty array

I am trying to make a search bar in order to find some cocktails by their name in my index. All I got is an empty array...
I believe my SQl request is not good... I'd need help please to make it working. Regards
Here is my code:
Search form:
<%= simple_form_for :query, url: cocktails_path, :method => :get do |f| %>
<%= f.input :search %>
<%= f.button :submit %>
<% end %>
cocktails controller:
def index
if params[:query].present?
#cocktails = Cocktail.all
#cocktail_search = #cocktails.where('cocktails.name LIKE ?', params[:query][:search])
binding.pry
else
#cocktails = Cocktail.all
end
end
the index view:
<% if #cocktail_search %>
<% #cocktail_search.each do |cocktail| %>
<div class="col-xs-12 col-sm-3">
<div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)),
url('<%= cl_image_path cocktail.photo %>');">
<h2 class="card-description"><%= link_to cocktail.name, cocktail_path(cocktail)%></h2>
</div>
</div>
<% end %>
<% else %>
<% #cocktails.each do |cocktail| %>
<div class="col-xs-12 col-sm-3">
<div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)),
url('<%= cl_image_path cocktail.photo %>');">
<h2 class="card-description"><%= link_to cocktail.name, cocktail_path(cocktail)%></h2>
</div>
</div>
<% end %>
<% end %>
This is my binding pry
#cocktail_search = #cocktails.where('cocktails.name LIKE ?', params[:query][:search])
If your table has column "name", then:
#cocktail_search = #cocktails.where('name LIKE ?', params[:query][:search])
should do the work.
When you're uncertain about what is wrong you can rails console and check things yourself, ex. Coctail.where("name like ?", "Mojito") and see if it returns expected result.
Since you use LIKE it seems like you want to find cocktails every if the search term matches only parts of the name. This can be archived by using % (see: Safe ActiveRecord like query).
Furthermore you might want to use a case-insensitive ILIKE instead of LIKE:
#cocktails.where('name ILIKE ?', "%#{params[:query][:search]}%")
Without the % you query is basically the same as
#cocktails.where(name: params[:query][:search])
that only returns exact matches.

MVC pagedlist.mvc and aspx view

I have got a page where im using the Nuget package pagedlist.mvc 3.0.18 (The latest version for MVC3). The problem is that when i'm listing the pages its makes my pages into a list of which look like
previous
1
2
3
4
next
Instead of < 1,2,3,4,5,6,7.. >
My view looks like
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IPagedList<News>>" %>
<%# Import namespace="PagedList" %>
<%# Import namespace="PagedList.Mvc" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table>
<tr>
<td>
<% foreach (var v in Model)
{%>
<%: v.Content %><br />
<%: v.DateCreated %><br />
<%: v.Email %><br />
<%} %>
<h2>News</h2>
<%: Html.PagedListPager(Model, page => Url.Action("News", new { page }), PagedListRenderOptions.Minimal)%>
Code behind looks like
public ActionResult News(int? page)
{
List<News> products = HomeBLL.GetNewsList(); //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?
var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
var onePageOfProducts = products.ToPagedList(pageNumber, 25); // will only contain 25 products max because of the pageSize
ViewBag.OnePageOfProducts = onePageOfProducts;
return View(onePageOfProducts);
}
Any suggestions on what could be wrong? The standard CSS is included as well.
You should include PagedList.css in your View (it is located in Content folder).
It seems that the newest package of pagedlist is NOT supported for the aspx views, unless you reach a low level of this package.
Include the css file PagedList.css inside HEAD tags in your _Layout page or at the top of your viewpage.

Adding Check box on Model Value condition in MVC Telerik grid

I am using teleik grid in MVC3, i am binding my grid data with model response value.
now my requrement is i have to add a checkbox in only those columns which status are completed.
Html.Telerik().Grid(Model.response)
.Columns(columns =>
{
columns.Bound(grid => grid.CaseStatus).Width(80);
columns.Bound(grid => grid.CaseID).Title("").Format("<input type='checkbox'name='checkedRecords' value='{0}' id ='{0}'/>").Encoded (false).Width(5);
})
i am binding my check box like this but i have to bind checkbox only with those rows, which status will completed.
please let me know how to check the value of CaseStatus here.
thanks
Use columns.Template to add checkbox. Like here:
columns.Template(o =>
{
%>
<input name="checkedRecords" type="checkbox" value="<%= o.CaseID %>"
<% if (checkedRecords.Contains(o.CaseID)) {
%> checked="checked" <%
} %>
/>
<%
}).Title("").Encoded(false).Width(5);
See more details here: http://demos.telerik.com/aspnet-mvc/grid/checkboxesserverside

How can I hide a search button and show a spinner while my AJAX partial loads in rails 3.1?

I have a partial which returns results from the Amazon-ecs API (searches for books). It takes about 2.5 seconds to return values, so I want to add a spinner (and ideally hide the search button) but I've had a tough time getting it to work.
I already have javascript refreshing the partial with the results. I'd just like the button to give me a spinner (and hide the search button) until the partial finishes reloading.
Here is my main view (index.html.erb) which renders a partial of search results, each of which is added temporarily as an object in AmazonItems:
<%= form_tag amazon_items_path, :method => :get, :remote => true do %>
<p>
<%= text_field_tag :query, params[:query] %>
<span id="spinner" style="display:none">
<%= image_tag 'ajax-loader.gif' %>
</span>
<span id="search_button">
<%= submit_tag "Search" %>
</span>
</p>
<% end %>
<h1>Searching Amazon</h1>
<div id="amazon_returned">
<%= render 'amazon_returned' %>
</div>
My only JS code is in the update.js.erb file, which looks like:
$("#amazon_returned").html("<%=j render 'amazon_returned' %>");
I'm sure this is easy, but I can't get it work. Thanks!
EDIT: my partial "_amazon_returned.html.erb" looks like
<table>
<% for amazon_item in #amazon_items %>
<td> amazon_item.title </td>
...ETC...
<% end %>
</table>
You can hook into the before event to do this:
$(function() {
$("form").on("ajax:before", function() {
$("#spinner, #search_button").toggle();
});
});
Then, in your update.js.erb, add this again to toggle them back:
$("#spinner, #search_button").toggle();
Went with Dylan's answer, added the toggle to the update.js.erb and inserted the following into the view:
<script>
$(function() {
$("#search_button").click(function() {
$("#spinner, #search_button").toggle();
});
});
</script>
Very cool! Thanks again for the help.

Resources