I have an ASP.net Core project. I want to send entire of a model that is a list of products, from a view, through "a" tag, to another get action in controller. I used TempData but it dis not work. Products model has a lot of properties and foreign keys.
Here is an example:
First action is:
public IActionResult ShowProducts(int side, MyViewModel myViewModel)
{
ViewBag.side = side;
if (side == 0)
{
List<Product> products = ShowAllProducts();
return View(products);
}
List<Product> products2 = FilterProducts(myViewModel);
return View(products2 );
}
List<Product> FilterProducts(MyViewModel myViewModel)
{
List<Product> Products = new List<Product>() {};
//some code...
return products;
}
List<Product> ShowAllProducts()
{
return _context.Products.Include(p => p.Table1).Include(p => p.Table2)
.Include(p => p.Table3).Include(p => p.Table4)
.Include(p => p.Table5).Include(p => p.Table6)
.Include(p => p.Table7).Include(p => p.Table8).Tolist();
}
Second Action is:
public IActionResult PrintProducts(List<Product> model)
{
return View(model);
}
ShowProducts View is:
@model IEnumerable<Product>
@*some tag*@
<a title="Print" asp-action="PrintProducts" asp-route-model ="@Model"></a>
@*some tag*@
In this example, model in second action is empty. Actually it doesn’t send.
I changed the view like this:
@model IEnumerable<Product>
@*some tag*@
<a title="Print" href="@Url.Action("PrintProducts", "Controller", new { model = @Model })"></a>
@*some tag*@
but this way didn’t work too.
I used TempData
and changed actions like this:
First action:
public IActionResult ShowProducts(int side, MyViewModel myViewModel)
{
ViewBag.side = side;
if (side == 0)
{
List<Product> products = ShowAllProducts();
TempData["MyModel"] = products;
return View(products);
}
List<Product> products2 = FilterProducts(myViewModel);
TempData["MyModel"] = products2;
return View(products2 );
}
Second Action is:
public IActionResult PrintProducts()
{
List<Product> model= TempData["MyModel"] as List<Product>;
return View(model);
}
but in this way, the first view didn’t load at all! I got status code 500.
Is there any way to solve this problem?
2
Answers
Pagination
Normally, you would use pagination if you want to show a lot of data. This essentially divides your data into multiple chunks of records called pages. Now, If you want to show all data on a single page, you can call the GET multiple times underwater to get all the data you want. This reduces the amount of data in a single call. It also makes things more scalable.
Filtering
Filtering is used, so the user can filter on certain criteria. For example, a search box. This would allow the user to enter a name, and you only return the items that match these criteria.
Ordering
If you are going to page the data, you also need to ensure the data is fetched in a particular order. If the data is in a random order for each call, the pages will contain different items each time they are fetched. It also allows your users to sort by different properties.
Example
Here is how I do this, it also contains filtering and sorting.
How to show this
On the HTML side of things, there are different solutions depending on how you want your user experience. A few options you see a lot on the internet:
More info
The Microsoft documentation has a very nice explanation:
https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-7.0
The tempdata’s provider is cookie-based by default. Most web clients such as web browsers enforce limits on the maximum size of each cookie and the total number of cookies. When using the cookie TempData provider, verify the app won’t exceed these limits. Consider the total size of the data. Account for increases in cookie size due to encryption and chunking.
So we don’t suggest you store 3000 records inside the tempdata , one possible way is storing the records ID and then inside another method query the data by using that ID.
Another way is storing the data inside the session, memory cache, or redis cache.
More details about how tempdata works and select which storage to store cache, you could refer to this article.