I'm using ASP.NET MVC3. Currently I have a blog. Its working well but the entries are now getting many so I need a archieve to group them monthly. The problem is I dont have an idea how to do it..
I'm thinking its on the datetime but I don't know the logic behind it.. I want to achieve something like this
September 2012 (1)
October 2012 (3)
December 2012 (0)
I would appreciate anyone who would help me. Thanks. :)
You could do it like this. I have the same problem before.
ArchiveRepository
public IQueryable<ArchiveListModel> AchiveList()
{
var ac = from Post in db.Posts
group Post by new { Post.DateTime.Year
, Post.DateTime.Month }
into dategroup
select new ArchiveListModel()
{
AchiveYear = dategroup.Key.Year,
AchiveMonth = dategroup.Key.Month,
PostCount = dategroup.Count()
};
return ac;
}
Controller
public ActionResult ArchiveBrowse(int AchiveYear, int AchiveMonth, int PostCount)
{
var archivemodel = (from a in db.Posts
where a.DateTime.Year == AchiveYear &&
a.DateTime.Month == AchiveMonth
select a).ToList();
return View(archivemodel);
}
View
#foreach (var item in Model)
{
#Html.ActionLink(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(item.AchiveMonth) + "" + item.AchiveYear + " (" + item.PostCount + ")",
"ArchiveBrowse", "Post", new
{
AchiveYear = item.AchiveYear,
AchiveMonth = item.AchiveMonth,
PostCount = item.PostCount
}, null)
}
Related
I have used TempData to pass data between controllers. If two users are working on the same page, User2 will get the tempdata value which is created by User1.
Below you can find the code.
Can you please help me to solve this issue.
VDRController.cs
public ActionResult RedirectToCDR(long VslMoveId, long VslVisitId, string VslNm, string ETDDtTmLoc, string statusCd)
{
//List<long> vslIds = new List<long>();
string vslIds = VslMoveId + "," + VslVisitId.ToString() + "," + statusCd;
TempData["VslMoveDetails"] = vslIds;
//DateTime MyDateTime;
//MyDateTime = new DateTime();
var strdatetime = Convert.ToDateTime(ETDDtTmLoc).ToString("dd-MMM-yyyy");
TempData["VslNameETD"] = VslNm + " " + strdatetime;
//return RedirectToAction("Index","VesselVisit");
return Json(Url.Action("Index", "CDR"));
}
CDRController.cs
public ActionResult Index()
{
if (TempData["VslMoveDetails"] != null)
{
string vslIds = TempData["VslMoveDetails"].ToString();
string[] Ids = vslIds.Split(new char[] { ',' });
if (Ids.Length == 3 && Ids[0] != null && Ids[1] != null)
{
ViewBag.VslMove_Id = Ids[0];
ViewBag.VslVisit_Id = Ids[1];
ViewBag.VisitStatus_Cd = Ids[2];
}
}
return View();
}
thanks in advance.
Your pages are being cached by IIS. For MVC applications you will need to add .cshtml to the list of file extensions that are not allowed to cache. Please see the following link for the solution to stop caching. (http://lionsden.co.il/codeden/?p=446)
public bool SaveValidTicketNos(string id,string[] ticketNos, string checkType, string checkMan)
{
bool result = false;
List<Carstartlistticket>enties=new List<Carstartlistticket>();
using (var context = new MiniSysDataContext())
{
try
{
foreach (var ticketNo in ticketNos)
{
Orderticket temp = context.Orderticket.ByTicketNo(ticketNo).SingleOrDefault();
if (temp != null)
{
Ticketline ticketline= temp.Ticketline;
string currencyType = temp.CurrencyType;
float personAllowance=GetPersonCountAllowance(context,ticketline, currencyType);
Carstartlistticket carstartlistticket = new Carstartlistticket()
{
CsltId = Guid.NewGuid().ToString(),
Carstartlist = new Carstartlist(){CslId = id},
LeaveDate = temp.LeaveDate,
OnPointName = temp.OnpointName,
OffPointName = temp.OffpointName,
OutTicketMan = temp.OutBy,
TicketNo = temp.TicketNo,
ChekMan = checkMan,
Type = string.IsNullOrEmpty(checkType)?(short?)null:Convert.ToInt16(checkType),
CreatedOn = DateTime.Now,
CreatedBy = checkMan,
NumbserAllowance = personAllowance
};
enties.Add(carstartlistticket);
}
}
context.BeginTransaction();
context.Carstartlistticket.InsertAllOnSubmit(enties);
context.SubmitChanges();
bool changeStateResult=ChangeTicketState(context, ticketNos,checkMan);
if(changeStateResult)
{
context.CommitTransaction();
result = true;
}
else
{
context.RollbackTransaction();
}
}
catch (Exception e)
{
LogHelper.WriteLog(string.Format("CarstartlistService.SaveValidTicketNos({0},{1},{2},{3})",id,ticketNos,checkType,checkMan),e);
context.RollbackTransaction();
}
}
return result;
}
My code is above. I doubt these code have terrible poor performance. The poor performance in the point
Orderticket temp = context.Orderticket.ByTicketNo(ticketNo).SingleOrDefault();
,actually, I got an string array through the method args,then I want to get all data by ticketNos from database, here i use a loop,I know if i write my code like that ,there will be cause performance problem and it will lead one more time database access,how can avoid this problem and improve the code performance,for example ,geting all data by only on databse access
I forget to tell you the ORM I use ,en ,the ORM is PlinqO based NHibernate
i am looking forward to having your every answer,thank you
using plain NHibernate
var tickets = session.QueryOver<OrderTicket>()
.WhereRestrictionOn(x => x.TicketNo).IsIn(ticketNos)
.List();
short? type = null;
short typeValue;
if (!string.IsNullOrEmpty(checkType) && short.TryParse(checkType, out typeValue))
type = typeValue;
var entitiesToSave = tickets.Select(ticket => new Carstartlistticket
{
CsltId = Guid.NewGuid().ToString(),
Carstartlist = new Carstartlist() { CslId = id },
LeaveDate = ticket.LeaveDate,
OnPointName = ticket.OnpointName,
OffPointName = ticket.OffpointName,
OutTicketMan = ticket.OutBy,
TicketNo = ticket.TicketNo,
ChekMan = checkMan,
CreatedOn = DateTime.Now,
CreatedBy = checkMan,
Type = type,
NumbserAllowance = GetPersonCountAllowance(context, ticket.Ticketline, ticket.CurrencyType)
});
foreach (var entity in entitiesToSave)
{
session.Save(entity);
}
to enhance this further try to preload all needed PersonCountAllowances
I am trying to get a record from database using linq but it keep return no record
it is very basic sql statment
select * where productid ='12553'
however the following code does not return any result. Please advise. thx you
private static IEnumerable<ProductModel> GetAllProduct(string productId)
{
using (var dc = new TestEntities())
{
var result = (from a in dc.Products
where a.productid == productId
select new ProductModel
{
ProductId = a.productid,
Name = a.ProductName
});
return result.Distinct().ToList();
}
}
You don't need projection here:
using (var dc = new TestEntities())
{
var result = from a in dc.Products
where a.productid == productId
select a;
return result.Distinct().ToList();
}
This is a part of my question which I deleted because its too broad
I created a ActionLink for my blog Archive but I'm having trouble with it because I'm calling multiple items inside it.
This is my codes which return an error message No overload for method ActionLink takes 7 arguments
#model IEnumerable <Project.Models.ArchiveListModel>
#foreach (var item in Model)
{
<br />
#Html.ActionLink(item.AchiveMonth, item.AchiveYear, item.PostCount, "ArchiveBrowse", "Post",
new { AchiveYear = item.AchiveMonth, ArchiveMonth = item.AchiveYear, PostCount = item.PostCount }, null)
}
This is my original codes but doesn't have a link it only gives a list
#foreach (var item in Model)
{
<br />
<li> #System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(item.AchiveMonth) #item.AchiveYear (#item.PostCount) </li>
}
</fieldset><br/>
output:
January 2013 (1)
February 2013 (1)
December 2012 (4)
Here's how I do it in my controller but I know its not working. T_T
public ActionResult Archive()
{
var archivelst = repository.AchiveList().ToList();
return View(archivelst);
}
//this one below is not working
public ActionResult ArchiveBrowse(string archive)
{
var achivemodel = db.Posts.Include("Posts").Single(a => a.Title == archive);
return View(achivemodel);
}
return View(achivemodel);
My ArchiveRepository
public IQueryable<ArchiveListModel> AchiveList()
{
var ac = from Post in db.Posts
group Post by new { Post.DateTime.Year, Post.DateTime.Month }
into dategroup
select new ArchiveListModel()
{
AchiveYear = dategroup.Key.Year,
AchiveMonth = dategroup.Key.Month,
PostCount = dategroup.Count()
};
return ac;
}
What's the correct way to call multiple items in the view?
What I'm trying here is to view the list of Posts under a specific month and year or something like Blog Archives.
Latest Update(working)
Finally I was able to make it work this is now a working one
Updated ArchiveRepository
public IQueryable<ArchiveListModel> AchiveList()
{
var ac = from Post in db.Posts
group Post by new { Post.DateTime.Year, Post.DateTime.Month }
into dategroup
select new ArchiveListModel()
{
AchiveYear = dategroup.Key.Year,
AchiveMonth = dategroup.Key.Month,
PostCount = dategroup.Count()
};
return ac;
}
Updated Controller
public ActionResult ArchiveBrowse(int AchiveYear, int AchiveMonth, int PostCount)
{
var archivemodel = (from a in db.Posts
where a.DateTime.Year == AchiveYear &&
a.DateTime.Month == AchiveMonth
select a).ToList();
return View(archivemodel);
}
Updated View
#foreach (var item in Model)
{
#Html.ActionLink(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(item.AchiveMonth) + "" + item.AchiveYear + " (" + item.PostCount + ")",
"ArchiveBrowse", "Post", new
{
AchiveYear = item.AchiveYear,
AchiveMonth = item.AchiveMonth,
PostCount = item.PostCount
}, null)
}
It's still not clear to me when you said "call multiple items" so here's my answer, assuming you want to make the list of months years (postcount) into a link. To get this output below
January 2013 (1)
February 2013 (1)
December 2012 (4)
you can concatenate the strings item.AchiveMonth, item.AchiveYear and item.PostCount:
#Html.ActionLink(item.AchiveMonth + " " + item.AchiveYear + " ("+ item.PostCount + ")",
"ArchiveBrowse",
"Post",
new {
AchiveYear = item.AchiveYear,
ArchiveMonth = item.AchiveMonth,
PostCount = item.PostCount },
null)
then in ArchiveBrowse, make sure that your parameters line up properly:
public ActionResult ArchiveBrowse(string AchiveYear, string ArchiveMonth, string PostCount)
{
//Not sure if you want to display posts in the clicked month/year
}
I want to display all records from table of current autorized user in my ASP.NET-MVC 3 + SQL Server 2008 app. But I have some problems:
this code with LINQ-request working good:
public ActionResult Index(todo obj)
{
string u = User.Identity.Name;
var th = (from tt in _db.todo select tt);
return View(th);
}
but this code not work:
public ActionResult Index(todo obj)
{
string u = User.Identity.Name;
var th = (from tt in _db.todo where obj.login == u select tt);
return View(th);
}
and this code is working good
if (u == obj.login) { ViewBag.res = "ok"; } else { ViewBag.res = "fail"; }
What I do wrong, please help me.
You probably want to run your where criteria against the table you're querying, instead of against the argument from the method, i.e.:
var th = (from tt in _db.todo where tt.login == u select tt);
instead of obj.login == u, try
obj.Contains(u)