I am having a bit of a struggle using linq and returning a list while trying to group.
In my view, I have the following:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Log>>" %>
<table>
<tr>
<th>
ID
</th>
<th>
User
</th>
<th>
NumberDialed
</th>
<th>
Date
</th>
<th>
Time
</th>
<th> </th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%: item.ID %>
</td>
<td>
<%: item.UserName %>
</td>
<td>
<%: item.NumberDialed %>
</td>
<td>
<%: item.Date %>
</td>
<td>
<%: item.Time %>
<%} %>
</td>
</table>
and in my controller I have:
public ActionResult Tenant()
{
LogEntities db = new LogEntities();
var s = from logs in db.Logs
group logs by logs.UserName;
return View(s.ToList());
}
when I do a normal select it works:
public ActionResult Tenant()
{
using (LogEntities db = new LogEntities())
{
var s = from logs in db.Logs
orderby logs.UserName
select logs;
return View(s.ToList());
}
}
Why is using Group By not working and why it is so different? I want to be able to display the data by groups. How do I accomplish this?
Thanks :)
I think that in order to do a group you need to specify into what you need it to be grouped
var s = (from logs in db.Logs
group logs by logs.UserName into us
select us).ToList();
Then us.Key gives you the username by which it is grouped and us gives all the items that are grouped by this username.
Then you can do:
foreach(var userGroup in s){
var userName = us.Key;
var logs = us.ToList();
}
Related
I am work on MVC 5 and i fill a dropdown with state name of Country through ViewData and also in my view page i show the every state record. But i want, After selection of Dropdown value in view show only selected state value. this is my code that's not working
Controller
public ActionResult Index(int? id)
{
ViewData["State"] = new SelectList(db.state, "state_id", "name");
if (id!=null)
return View(db.state.Where(x => x.state_id == id));
else
return View(db.state.ToList());
}
View
#Html.DropDownList("ddlstate", (SelectList)ViewData["State"], "Select State", new { onchange = "Index(this.value);", #class = "form-control" })
<table class="table" id="statedetails">
<thead>
<tr>
<th>
State ID
</th>
<th>
State Name
</th>
<th>
City
</th>
<th>
Population
</th>
<th>
Education %
</th>
<th></th>
</tr>
</thead>
#foreach (var item in Model)
{
<tbody>
<tr>
<td>
#Html.DisplayFor(modelItem => item.stateid)
</td>
<td>
#Html.DisplayFor(modelItem => item.statename)
</td>
<td>
#Html.DisplayFor(modelItem => item.city)
</td>
<td>
#Html.DisplayFor(modelItem => item.population)
</td>
<td>
#Html.DisplayFor(modelItem => item.eduction)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.offer_id }) |
#Html.ActionLink("Details", "Details", new { id = item.offer_id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.offer_id })
</td>
</tr>
</tbody>
}
</table>
javascript
<script type="text/javascript">
function Index(id) {
$.ajax({
url: "#Url.Action("Index","State")",
type: "POST",
data: { "id": id }
});
}
</script>
Maybe this question was asked a lot but not in the same way so here is my problem and I hope someone can help.
After having retrieved some research rows from database and returned the results into a List which I have bound to the my Spring MVC Controller Model:
if(!result.hasErrors())
{
try{
List<Question> questionlist = questionservice.findByCategoryAndLevel(questionform.getCategory(),questionform.getLevel());
model.addAttribute("questionlist",questionlist);
return "addExam";
}catch(NullPointerException e)
{
return "redirect:/admin/addexam";
}
}
Here is my view:
<form:form action="addexam" method="POST" modelAttribute="questionlist">
<table class="table table-striped table-bordered table-hover" id="sample_1">
<thead>
<tr>
<th class="table-checkbox">
<input type="checkbox" class="group-checkable" data-set="#sample_1 .checkboxes"/>
</th>
<th>
Category
</th>
<th>
level
</th>
<th>
Type of question
</th>
<th>
Status
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<c:forEach items="${questionlist}" var="question">
<c:choose>
<c:when test="${question.isVisible()}">
<tr class="odd gradeX">
<td>
<input type="checkbox" class="checkboxes" />
</td>
<td>
${question.category.getCategoryName() }
</td>
<td>
${question.level }
</td>
<c:choose>
<c:when test="${question.isSingleChoiceQuestion() }">
<td>Question à choix unique</td>
</c:when>
<c:otherwise>
<td>Question à choix mutiple</td>
</c:otherwise>
</c:choose>
<td class="center">
<c:choose>
<c:when test="${question.getState() == 'Desactivated'}">
<span class="label label-sm label-default"> Desactivated </span>
</c:when>
<c:when test="${question.getState() == 'Activated'}">
<span class="label label-sm label-success"> Activated </span>
</c:when>
<c:when test="${question.getState() == 'Pending'}">
<span class="label label-sm label-warning"> Pending </span>
</c:when>
</c:choose>
</td>
<td>
View
</td>
</tr>
</c:when>
</c:choose>
</c:forEach>
</tbody>
</table>
</form:form>
Now how do I submit the selected items?
yes i wanted to display the question list and then pick the ones i needed via a checkbox
i created a form, it holds the list of Idquestions
public class checkedquestion {
private List<Long> listIdQuestion ;
//getter & setter
}
then added a path attribute to checkbox like below :
<form:form action="submit" method="POST" modelAttribute="checkedquestion">
// .......
<td>
<form:checkbox value="${question.idQuestion}" path="listIdQuestion"/>
</td>
By submitting the list of id i have the ones i needed :
public String add(#ModelAttribute ("checkedquestion") #Valid CheckedQuestion checkedquestion , BindingResult result )
{
if(!result.hasErrors())
{
List<Long> list = checkedquestion.getListIdQuestion();
List<Question> questionlist = questionservice.getQuestion(list);
}
}
it seems to work fine
Give a id/name to input checkbox type as below
<input type="checkbox" class="checkboxes" id="someId" name="someId" value="uniqueValueToEachCheckBox"/>
Then after submitting your form, you can able to access your selected checkbox values in your controller as below.
request.getParameterValues("someId");
I am new to CanJS and was trying to learn via tutorials.. Once place where I got stuck was when I converted the todo tutorial code to use table instead of UL/LI combination.
May be I am making some very small mistake.. but could not find out the same for last 3 days.. Can someone help out?
The jsFiddle for the short version of todo tutorial (which now uses tables) is here where you can see the problems.
The crux of the code from ejs is here..
<script type='text/ejs' id='todosEjs'>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>status</th>
</tr>
</thead>
<tbody>
<!-- bind to changes in the todo list -->
<% this.each(function( todo ) { %>
<!-- add the todo to the element's data -->
<tr <%= (el) -> el.data('todo',todo) %>>
<td>
<input type="checkbox" <%= todo.attr('complete') ? 'checked' : '' %>>
</td>
<td><%= todo.attr('name') %></td>
<td><%= todo.attr('id') %></td>
</tr>
<% }) %>
</tbody>
</table>
</script>
Looking forward towards helping hands :-)
UPDATE: This was the result of a bug which is now fixed
It looks as if the comments are the problem here. Removing them makes it work, see the updated Fiddle:
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>status</th>
</tr>
<% this.each(function( todo ) { %>
<tr <%= (el) -> el.data('todo',todo) %>>
<td>
<input type="checkbox" <%= todo.attr('complete') ? 'checked' : '' %>>
</td>
<td><%= todo.attr('name') %></td>
<td><%= todo.attr('id') %></td>
</tr>
<% }) %>
CS0234: The type or namespace name 'Helpers' does not exist in the namespace 'UTEPSA' (are you missing an assembly reference?)
Here is the view code that's trying to inherit:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<UTEPSA.Helpers.PaginatedList<Area>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<table>
<tr>
<th></th>
<th>
ID
</th>
<th>
IDJefe
</th>
<th>
Nombre
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "Edit", new { id=item.ID }) %> |
<%: Html.ActionLink("Details", "Details", new { id=item.ID })%> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.ID })%>
</td>
<td>
<%: item.ID %>
</td>
<td>
<%: item.IDJefe %>
</td>
<td>
<%: item.Nombre %>
</td>
</tr>
<% } %>
</table>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
</asp:Content>
And a screenshot of my solution:
How can I correctly reference my PaginatedList? Since the view is inheriting from my thing I want, I'd expect to have strongly typed Model to work with. But intellisense isn't working so somethings wrong.
You must add the assembly to both the Project Reference list as well as the <system.web><compilation><assemblies> section of web.config.
have an action that generates active vacancies. The code is below;
public ViewResult OpenVacancies()
{
var openvacancies = db.GetActiveVacancies();
return View(openvacancies);
}
I want to use this list on several pages so i guess the best thing to use is html.renderaction (please correct me if i am wrong here).
Please note that the view and .ascx control are in an Area.
I then created a view by right clicking inside the action and create a .ascx and a strongly typed view of Vacancy. I chose a view content of "List".
I then added this line to the required page;
Please note that the view and .ascx control are in an Area.
The error i got is;
The type or namespace name 'Vacancy' could not be found (are you missing a using directive or an assembly reference?)
the .ascx code is below;
>" %>
<table>
<tr>
<th></th>
<th>
VacancyID
</th>
<th>
JobTitle
</th>
<th>
PositionID
</th>
<th>
LocationID
</th>
<th>
JobDescription
</th>
<th>
JobConditions
</th>
<th>
Qualifications
</th>
<th>
RequiredSkills
</th>
<th>
Certifications
</th>
<th>
AdvertDate
</th>
<th>
AdvertExpiryDate
</th>
<th>
Status
</th>
<th>
StaffLevel
</th>
<th>
LineManagerEmail
</th>
<th>
ApprovalFlag
</th>
<th>
RequisitionDate
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id=item.VacancyID }) %> |
<%= Html.ActionLink("Details", "Details", new { id=item.VacancyID })%> |
<%= Html.ActionLink("Delete", "Delete", new { id=item.VacancyID })%>
</td>
<td>
<%= Html.Encode(item.VacancyID) %>
</td>
<td>
<%= Html.Encode(item.JobTitle) %>
</td>
<td>
<%= Html.Encode(item.PositionID) %>
</td>
<td>
<%= Html.Encode(item.LocationID) %>
</td>
<td>
<%= Html.Encode(item.JobDescription) %>
</td>
<td>
<%= Html.Encode(item.JobConditions) %>
</td>
<td>
<%= Html.Encode(item.Qualifications) %>
</td>
<td>
<%= Html.Encode(item.RequiredSkills) %>
</td>
<td>
<%= Html.Encode(item.Certifications) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.AdvertDate)) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.AdvertExpiryDate)) %>
</td>
<td>
<%= Html.Encode(item.Status) %>
</td>
<td>
<%= Html.Encode(item.StaffLevel) %>
</td>
<td>
<%= Html.Encode(item.LineManagerEmail) %>
</td>
<td>
<%= Html.Encode(item.ApprovalFlag) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.RequisitionDate)) %>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "Create") %>
</p>
First, if you simply want to reuse your view on multiple pages, you should be using a shared partial view (place it in the Shared folder) with <% Html.RenderPartial("OpenVacancies"); %>.
Second, based on your code snippet, your partial view doesn't appear to be strongly typed. Instead of
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
you will want something like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Vacancy>>" %>