ASP.NET MVC Paging Table with only one datasource refresh? - c#

If you would do this Paging approach everything works finde. My problem is that i don't want get the dataset each time. I only want to do it once. How can i do that?
Table:
#model PagedList.IPagedList<ViewModel.QuestionViewModel>
#using PagedList.Mvc;
<table class="table">
<tr class="table-head">
<th>
<p>Sender</p>
</th>
<th>
<p>Subject</p>
</th>
<th>
<p>Received</p>
</th>
<th>
<p>HasAttachment</p>
</th>
<th></th>
</tr>
#foreach (var item in Model.Inbox) {
<tr class="mail-row" onclick="location.href = '#Url.Action("Index", "MailContent", new { id = item.Id })'">
<td>
#Html.DisplayFor(modelItem => item.From)
</td>
<td>
#Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
#Html.DisplayFor(modelItem => item.Received)
</td>
<td>
#Html.DisplayFor(modelItem => item.HasAttachments)
</td>
<td>
#Html.DisplayFor(modelItem => item.AssetClass)
</td>
<td>
#Html.DisplayFor(modelItem => item.InProgressBy)
</td>
#*<td>
#html.actionlink("edit", "edit", new { /* id=item.primarykey */ }) |
#html.actionlink("details", "details", new { /* id=item.primarykey */ }) |
#html.actionlink("delete", "delete", new { /* id=item.primarykey */ })
</td>*#
</tr>
}
</table>
Page #(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of #Model.PageCount
#Html.PagedListPager( Model, page => Url.Action("MailList", new { page }) )
So this is the table, but each time i click on the next button it will go back to the controller which gets all mails again. due to a larger set of data its inefficent to load it every time.
Controller:
MailboxController
public ActionResult MailList(string id, int? page) {
//TODO: get list
//get Maillist for mailbox
context = new InboxToolContext();
context.Mails.MailboxType = id;
Console.Write(i);
i = 2;
int pageSize = 20;
int pageNumber = (page ?? 1);
return View(context.Mails.All().ToPagedList(pageNumber,pageSize));
}

Related

ASP.NET MVC Two models in ove view, code change

I had this code and it worked fine:
#model IEnumerable<Moviestore.Models.Movie>
#{
ViewBag.Title = "Index";
}
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.Genre)
</th>
<th>
#Html.DisplayNameFor(model => model.Author)
</th>
<th>
#Html.DisplayNameFor(model => model.Year)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
if (item.IsDeleted == 0)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Author)
</td>
<td>
#Html.DisplayFor(modelItem => item.Year)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.MovieID }) |
#Html.ActionLink("Details", "Details", new { id = item.MovieID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.MovieID })
</td>
</tr>
}
}
</table>
But I needed to add one more model, I done that with Tuple In this way:
#using Moviestore.Models;
#model Tuple<Movie, User>
#{
ViewBag.Title = "Index";
}
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(tuple => tuple.Item1.Title)
</th>
<th>
#Html.DisplayNameFor(tuple => tuple.Item1.Genre)
</th>
<th>
#Html.DisplayNameFor(tuple => tuple.Item1.Author)
</th>
<th>
#Html.DisplayNameFor(tuple => tuple.Item1.Year)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
if (item.IsDeleted == 0)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Author)
</td>
<td>
#Html.DisplayFor(modelItem => item.Year)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.MovieID }) |
#Html.ActionLink("Details", "Details", new { id = item.MovieID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.MovieID })
</td>
</tr>
}
}
</table>
But now I have problem with bottom half of code.
From #foreach
I don't know what I need to put instead of #foreach (var item in Model)
And will then #Html.DisplayFor(modelItem => item.Title) also need some changes.
Sorry for long post, I tried to explain the problem as best as I can.
As I mentioned in the comments, the appropriate way to handle this situation (needing more than one model on a particular View page) is to use a ViewModel which contains the models you need (see here for reference).
To answer your specific question, though, you can access each item of the Tuple by its position, e.g. if your model is Tuple<Movie, User>, then you can access the Movie object by model.Item1 and the User object by model.Item2, etc.
But I strongly recommend you take the ViewModel approach that I linked instead.
Hi here is the viewmodel for your requirement:
public class MovieUserViewModel
{
public IEnumerable<Movie> Movie { get; set; }
public IEnumerable<Users> Users { get; set; }
}
With the above viewmodel , you can easily access each class
#foreach (var item in Model.Movie)
#foreach (var item in Model.Users)
Additional info : Understanding ViewModel in ASP.NET MVC
Thanks
Karthik

How to add a where clause when returning a list in a view Mvc5

Is it possible to add a where clause in the view rather than the controller as I don't want to change the retrieval of information in the controller. I currently am retrieving a list of restaurants but want to only show restaurants where the logged in users email is the same as the restaurants email.
<div class="col-xs-12">
<table class="table table-condensed table-striped">
<tr>
<th>
#Html.DisplayNameFor(model => model.Rating)
</th>
<th>
#Html.DisplayNameFor(model => model.RestaurantName)
</th>
<th>
#Html.DisplayNameFor(model => model.County)
</th>
<th>
#Html.DisplayNameFor(model => model.RestaurantType)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.DisplayFor(modelItem => item.RestaurantName)
</td>
<td>
#Html.DisplayFor(modelItem => item.County)
</td>
<td>
#Html.DisplayFor(modelItem => item.RestaurantType)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.RestaurantID }) |
#Html.ActionLink("Details", "Details", new { id = item.RestaurantID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.RestaurantID })
</td>
</tr>
}
</table>
</div>
If you really don't want to change the logic for retrieving the list, you could use a ViewModel with a List property, returning the specified list using the Getter.
public List<Restaurant> MyRestaurants
{
get { return Restaurants.Where(x => x.RestaurantEmail == UserEmail); }
}

increment in asp.net mvc razor view

I want to display incremental values at the first column of following code inside Sr. No. column in ASP.net MVC Razor view. I am doing following but its not working. It showing 0++; only in that column. what wrong I am doing.
#{int i = 0;}
<table class="table">
<tr>
<th>Sr No.</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.ModifiedDate)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<span>#i++;</span>
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.ModifiedDate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.CurrencyCode }) |
#Html.ActionLink("Details", "Details", new { id = item.CurrencyCode }) |
#Html.ActionLink("Delete", "Delete", new { id = item.CurrencyCode })
</td>
</tr>
}
In razor syntax you should create C# scope for C# logical statemens. Can you try the the code below;
<td>
<span>#(i++)</span>
</td>
You can not use like this.
<td>
<span>#i++;</span>
</td>
you need to write your code as like :
<td>
<span>#i</span>
</td>
You need to increase value of i after <tr> tag.
I have given solution with your code.
#{
var i = 1; #*sr no will start from 1*#
}
<table class="table">
<tr>
<th>Sr No.</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.ModifiedDate)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<span>#i</span>
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.ModifiedDate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.CurrencyCode }) |
#Html.ActionLink("Details", "Details", new { id = item.CurrencyCode }) |
#Html.ActionLink("Delete", "Delete", new { id = item.CurrencyCode })
</td>
</tr>
i++;
}
Hope it will work fine.
This should work:
#{int i = 0;}
<table class="table">
<tr>
<th>Sr No.</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.ModifiedDate)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<span>#(++i)</span>
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.ModifiedDate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.CurrencyCode }) |
#Html.ActionLink("Details", "Details", new { id = item.CurrencyCode }) |
#Html.ActionLink("Delete", "Delete", new { id = item.CurrencyCode })
</td>
</tr>
}
As per #Stephen Muecke suggested you can use:
<span>#(i++)</span>
Or you can use like this:
<span>#i</span>
i++;

Show the result of an int method in the Index View

I`ve made a simple method to count how many Albums do I have in a table, my only question is simple, I want to show the result of the Count method in the AlbumIndex view. Lets say I want to show a label: "Total" and the number besides it, how do I do this in the View?
This is the method(inside a class called Operations, separated from Controllers and Models):
public int AlbumCounter()
{
int x = db.Albums.Count();
return x;
}
This is the ActionResult Method in my AlbumsController(I don`t know what to do, I want to show the result in the Index View)
public ActionResult AlbumCounter()
{
return
}
and here is the Index View:
#model IEnumerable<AlexMusicStore.Models.Album>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Artist.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.AlbumName)
</th>
<th>
#Html.DisplayNameFor(model => model.DateCreated)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Artist.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.AlbumName)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateCreated)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.AlbumID }) |
#Html.ActionLink("Details", "Details", new { id=item.AlbumID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.AlbumID })
</td>
</tr>
}
</table>

How to sort table in asp.net razor

As Title, I would like to sort data table
I want to sort from current datetime ---> pass datetime
I'm a new in this programming please help :)
thank you
Here is my code
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.SectorID)
</th>
.
.
.
<th>
#Html.DisplayNameFor(model => model.DateTime)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.SectorID)
</td>
.
.
.
<td>
#Html.DisplayFor(modelItem => item.DateTime)
</td>
<td>
#Html.ActionLink("Details", "Details", new { id=item.ID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
just order the model in a new orderedCollection here is in your view
or you can do the somehing from your controller
View
#{
var modelOrdered = Model.OrderByDescending(m=>m.DateTime);
}
Controller
var model = context.collections.OrderByDescending(m => m.DateTime);
return View(model);
//and here just iterate
#foreach (var item in modelOrdered) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.SectorID)
</td>
.
.
.
<td>
#Html.DisplayFor(modelItem => item.DateTime)
</td>
<td>
#Html.ActionLink("Details", "Details", new { id=item.ID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
The view is not the right place for manipulating data. You should do your ordering in your controller method, responsible for this view.
//this is where you fetch your data. Just add an .OrderBy or .OrderByDescending
var data = context.SomeEntity.OrderByDescending(ent => ent.DateTime);
return View(data);

Categories