How to Group By Razor view by Month - asp.net-mvc

Im using MVC razor view
How do I group by my index view basen on Month in TrDate ??
My Index Controller:
private InvoiceMasterEntities db = new InvoiceMasterEntities();
public ActionResult Index(int compID, string searchBy, string search, int? page, string sortBy)
{
ViewBag.SortDateParam = string.IsNullOrEmpty(sortBy) ? "Date desc" : "";
ViewBag.SortStatusParam = sortBy == "Status" ? "Status desc" : "Status";
var detail = db.DetailPengirimen.AsQueryable();
if (searchBy == "CustID")
{
detail = detail.Where(b => b.CustID.Contains(search) || search == null);
}
else
{
detail = detail.Where(a => a.Customer.Fullname.Contains(search) || search == null);
}
detail = detail.Where(a => a.CompID == compID);
switch (sortBy)
{
case "Date desc":
detail = detail.OrderByDescending(a => a.TrDate);
break;
case "Status desc":
detail = detail.OrderByDescending(a => a.Status.Status1);
break;
case "Status":
detail = detail.OrderBy(a => a.Status.Status1);
break;
default:
detail = detail.OrderBy(a => a.TrDate);
break;
}
return View(detail.ToPagedList(page ?? 1, 10));
}
My Index View:
#using PagedList;
#using PagedList.Mvc;
#model IPagedList<InvoiceTracker.Models.DetailPengiriman>
<h2>Couriers List</h2>
<p>
#Html.ActionLink("Insert Data", "Create", new { compID = Request.QueryString["compID"] })
</p>
<p>
#using (Html.BeginForm("Index", "DetailPengiriman", new { compID = Request.QueryString["compID"] }, FormMethod.Post))
{
<b>Search By:</b>
#Html.RadioButton("searchBy", "CustID", true) <text>CustomerID</text>
#Html.RadioButton("searchBy", "Fullname") <text>Customer Name</text>
<br />
#Html.TextBox("search")
<input type="submit" value="Search" />
}
</p>
<table class="table">
<tr>
<th>
#Html.ActionLink("Transaction Date", "Index", new
{
sortBy = ViewBag.SortDateParam,
searchBy = Request.QueryString["searchBy"],
search = Request["search"],
compID = Request["compID"]
})
</th>
<th>
CustomerID
</th>
<th>
Customer Name
</th>
<th>
Invoice
</th>
<th>
Nama Penerima
</th>
<th>
Status Penerima
</th>
<th>
#Html.ActionLink("Status", "Index", new
{
sortBy = ViewBag.SortStatusParam,
searchBy = Request.QueryString["searchBy"],
search = Request["search"],
compID = Request["compID"]
})
</th>
<th>
Status Date
</th>
<th>
Image
</th>
<th>Actions</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.TrDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Customer.CustID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Customer.Fullname)
</td>
<td>
#Html.DisplayFor(modelItem => item.InvoiceNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.NamaPenerima)
</td>
<td>
#Html.DisplayFor(modelItem => item.StatusPenerima)
</td>
<td>
#Html.DisplayFor(modelItem => item.Status.Status1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Status.StatDate)
</td>
<td>
#if (item.Image.Image1 != null)
{
var base64 = Convert.ToBase64String(item.Image.Image1);
var imgSrc = ($"data:image/png;base64,{base64}");
<img src="#imgSrc" width="50" height="50"
onMouseOut="zoomtoggle('50','50px','50px','50px',this);"
onMouseOver="zoomtoggle('400px','200px','400px','200px',this);"/>
}
else
{
<text> No Image Available</text>
}
</td>
<td>
#Html.ActionLink("Edit", "Edit", new
{
id = item.TransID,
custID = item.CustID,
statID = item.StatusID,
imgID = item.ImageID,
invoNo = item.InvoiceNo
}) |
#Html.ActionLink("Delete", "Delete", new
{
id = item.TransID,
custID = item.CustID,
statID = item.StatusID,
imgID = item.ImageID,
invoNo = item.InvoiceNo
})
</td>
</tr>
}
</table>
#Html.PagedListPager(Model, page => Url.Action("Index", new
{
page,
searchBy = Request.QueryString["searchBy"],
search = Request.QueryString["search"],
sortBy = Request["sortBy"],
compID = Request["compID"]
}),
new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded })
I want the view listed the data from database group by month in TrDate attribute
TrDate data are from datetime.now
TrDate data type is DateTime

Related

My sorting is not working in razor view but in my code i am getting the right result

I have an application in which i have departments view the department table have Id,name,Active and date created columns, I want to sort based on name and date so i wrote the below code in my index controller.
I have tried to pass query parameter and it works fine and my query is generted 100% right but on view it is not showing the right result
public ActionResult Index(string sortOrder)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
var dept = from s in db.Departments
select s;
switch (sortOrder)
{
case "name_desc":
dept = dept.OrderByDescending(s => s.Department_Title);
break;
case "Date":
dept = dept.OrderBy(s => s.Date_Created);
break;
case "date_desc":
dept = dept.OrderByDescending(s => s.Date_Created);
break;
default:
dept = dept.OrderBy(s => s.Department_Title);
break;
}
return View(db.Departments.ToList());
}
below is my razor view
<table class="table table-condensed">
#*<tr>
<th>
#Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
First Name
</th>
<th>
#Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
</th>
<th></th>
</tr>*#
<tr>
<th>
#Html.ActionLink("Department Name","Index",new {sortOrder=ViewBag.NamesortParm })
</th>
<th>
#Html.DisplayNameFor(model => model.Status)
</th>
<th>
#Html.ActionLink("Creation Date","Index",new {sortOrder=ViewBag.DateSortParm })
</th>
#*<th>
#Html.DisplayNameFor(model => model.Date_Modified)
</th>*#
<th>Edit/Delete</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Department_Title)
</td>
<td>
#if (item.Status != null)
{
#(item.Status.Value ? Html.Raw("ACTIVE") : Html.Raw("NOT ACTIVE"))
}
#*#Html.DisplayFor(modelItem => item.Status)*#
</td>
<td>
#Html.DisplayFor(modelItem => item.Date_Created)
</td>
#*<td>
#Html.DisplayFor(modelItem => item.Date_Modified)
</td>*#
<td colspan="3">
#Html.ActionLink("Edit", "Edit", new { id = item.Department_Id }, new { onclick = "return confirm('Are you sure you wish to delete?');", #class = "btn btn-sm btn-info" })
#*#Html.ActionLink("Details", "Details", new { id=item.Department_Id }) |*#
#Html.ActionLink("Delete", "Delete", new { id = item.Department_Id }, new { onclick = "return confirm('Are you sure you wish to delete?');", #class = "btn btn-sm btn-danger" })
</td>
</tr>
}
</table>
below is my query which is generated based on department_name click
SELECT
[Extent1].[Department_Id] AS [Department_Id],
[Extent1].[Department_Title] AS [Department_Title],
[Extent1].[Status] AS [Status],
[Extent1].[Date_Created] AS [Date_Created],
[Extent1].[Date_Modified] AS [Date_Modified]
FROM [dbo].[Departments] AS [Extent1]
ORDER BY [Extent1].[Department_Title] DESC
it should sort the result in the view ~
sorry i was making a normal mistake instead of returning the filtered list i was returning return View(db.Departments.ToList()); BUT IT SHOULD BE return View(dept.ToList());
You have to return your var dept variable since it already contains your sorted list in your switch case the db.department will return you the actual list of department which is inserted in the database. so instead of db.database.toList() use dept.ToList();

how to make model into IEnumerable for the view [duplicate]

This question already has answers here:
The model item passed into the dictionary is of type .. but this dictionary requires a model item of type
(7 answers)
Closed 5 years ago.
This is my ActionResult
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
return View(ee);
}
View is
#model IEnumerable<trydbconnectMvc.Models.Empoyee>
#{
ViewBag.Title = "mytry";
}
<h2>mytry</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.LastName)
</th>
<th>
#Html.DisplayNameFor(model => model.MiddleName)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th>
#Html.DisplayNameFor(model => model.Salary)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.MiddleName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email)
</td>
<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>
</tr>
}
</table>
I am getting error
" The model item passed into the dictionary is of type 'trydbconnectMvc.Models.Empoyee', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[trydbconnectMvc.Models.Empoyee]'."
I am new to mvc. How to solve this?
It seems you're returning only one entity, not a IEnumerable. Just change it in your view:
#model trydbconnectMvc.Models.Empoyee
#{
ViewBag.Title = "mytry";
}
<h2>mytry</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.LastName)
</th>
<th>
#Html.DisplayNameFor(model => model.MiddleName)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th>
#Html.DisplayNameFor(model => model.Salary)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
<tr>
<td>
#Html.DisplayFor(modelItem => #Model.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.MiddleName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Address)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Salary)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Email)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=#Model.Id }) |
#Html.ActionLink("Details", "Details", new { id=#Model.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=#Model.Id })
</td>
</tr>
}
</table>
or if you want, just change in your Controller:
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
var list = new List<Empoyee>(){ ee };
return View(list );
}
and keep your current view.
If you must use an IEnumerable, you can make a List<> and pass it in
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
List<Employee> employees = new List<Employee>();
employees.add(ee);
return View(employees);
}

MVC 4 Paging error

using this controller I am getting the error
the model item passed into the dictionary is of type 'System.Collections.Generic, but this dictionary requires a model item of type 'PagedList.IPagedList`
public ViewResult Index(string searchstring, string currentFilter, int? page)
{
if (searchstring == null && currentFilter == null && !page.HasValue)
{
// No parameters
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToList());
}
// Regular code here down
if (searchstring != null)
{
page = 1;
}
else
{
searchstring = currentFilter;
}
ViewBag.CurrentFilter = searchstring;
var teachers = from r in db.AdulLiteracyTeachers select r;
if (!string.IsNullOrEmpty(searchstring))
{
teachers = teachers.Where(r => r.ALTName.ToUpper().Contains(searchstring.ToUpper()));
}
teachers = teachers.OrderBy(r => r.ALTName);
int pagesize = 10;
int pageNo = (page ?? 1);
return View(teachers.ToPagedList(pageNo, pagesize));
}
my view index.cshtml is :
#using PagedList;
#using PagedList.Mvc;
#model IPagedList<LiteracyPayroll.Models.AdulLiteracyTeachers>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using (Html.BeginForm("Index","AdultLiteracyTeachers",FormMethod.Get))
{
<p>
Find By Teacher Name: #Html.TextBox("searchstring",ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.First().District.DistName)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTCLC)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTGEN)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTCNIC)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTDOB)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTName)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTFName)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTAppNo)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTDes)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTQuali)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTexp)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTDom)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTdoc)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTBG)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTAddress)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTcontact)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTBankno)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTBankname)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTBcode)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTNTN)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTPay)
</th>
<th>
#Html.DisplayNameFor(model => model.First().Image)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.District.DistName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTCLC)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTGEN)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTCNIC)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTDOB)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTFName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTAppNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTDes)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTQuali)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTexp)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTDom)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTdoc)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTBG)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTAddress)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTcontact)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTBankno)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTBankname)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTBcode)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTNTN)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTPay)
</td>
<td>
<img src="#Url.Content("~/Content/Uploads/" + "/" + item.Image + "")" alt="#item.Image" width="100px" height="100px" />
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ALTid }) |
#Html.ActionLink("Details", "Details", new { id=item.ALTid }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ALTid })
</td>
</tr>
}
</table>
<div>
Page #(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) Of #Model.PageCount
#Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
</div>
The problem is here in below line when you check that "searchstring" and "currentFilter" is null. Currently it look like this -
public ViewResult Index(string searchstring, string currentFilter, int? page)
{
if (searchstring == null && currentFilter == null && !page.HasValue)
{
// No parameters
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToList());
}
// Regular code here down
blah... blah..
}
Change your code to say this instead should fix it:
// Initialize the pagesize and pageNo at the starting of the function
int pagesize = 10;
int pageNo = (page ?? 1);
if (searchstring == null && currentFilter == null && !page.HasValue)
{
// No parameters
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToPagedList(pageNo, pagesize));
}
// Regular code here down
.......

ASP.NET MVC join tables view result error

im trying to join two table and show the results in a strongly-typed list view in with columns from two tables,
public ActionResult OrderDetails(int id) {
var query = from o in db.OrderDetails
join a in db.Albums on o.AlbumId equals a.AlbumId
where o.OrderId == id
select new OrderDetail() {
OrderId = o.OrderId,
AlbumId = o.AlbumId,
Quantity = o.Quantity,
UnitPrice = o.UnitPrice,
Album = new Album {
Title = a.Title
}
};
return View(query);
}
Here is my view
#model IEnumerable<QWERK.OrderDetail>
#{
ViewBag.Title = "OrderDetails";
}
<h2>OrderDetails</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.OrderId)
</th>
<th>
#Html.DisplayNameFor(model => model.AlbumId)
</th>
<th>
#Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
#Html.DisplayNameFor(model => model.UnitPrice)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.OrderId)
</td>
<td>
#Html.DisplayFor(modelItem => item.AlbumId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
#Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
#Html.DisplayFor(modelItem => item.Album.Title)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.OrderDetailId }) |
#Html.ActionLink("Details", "Details", new { id=item.OrderDetailId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.OrderDetailId })
</td>
</tr>
}
</table>
But i keep getting a NotSupportedException
Im pretty sure im getting the error because im using the OrderDetails Model but how can i get around this to show multiple columns from different tables???? any help would be greatly appreciated...thanks

How to pass a list of objects to a [HTTPPost]controller parameter, in ASP.Net MVC?

i have the next view:
#model IEnumerable<L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer>
#using (Html.BeginForm("ExpenseMonthlyTransferProcessing", "BudgetTransfer", Model.ToList())){
<table class ="divTable">
<tr>
<th>Transferir</th>
<th>
Clave
</th>
<th>
Monto
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.CheckBoxFor(x => item.MTR_Bool, new { #class = "checkMTR", #checked = "checked" })
</td>
<td>
#Html.TextBoxFor(x => item.MTR_Key, new {#class = "longInput" })
</td>
<td>
#String.Format("{0:F}", item.MTR_Amount)
</td>
</tr>
}
</table>
}
and my controller like this
[HttpPost]
public ActionResult ExpenseMonthlyTransferProcessing(List<MTR_MonthlyTransfer> lstMtr)
{ return View(lstMTR); }
But when i do the post my list is null, how can i send my list through the submit button ?
You should change the #model to an array (L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]) or something else that implements IList<>:
#model L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]
#for (var i = 0; i < Model.Length; i ++) {
<tr>
<td>
#Html.CheckBoxFor(x => Model[i].MTR_Bool, new { #class = "checkMTR", #checked = "checked" })
</td>
<td>
#Html.TextBoxFor(x => Model[i].MTR_Key, new {#class = "longInput" })
</td>
<td>
#String.Format("{0:F}", item.MTR_Amount)
</td>
</tr>
receive a FormCollection and parse the items in it manually
Use F12 to check the post in your navigator to see if it are sending the content you expected.

Resources