I have an application in ASP.NET where I want to pass this list of objects to my Controller. Right now what the application is doing is populating a list via sql query and then loads said list to the view. Up next, I divvy up the list into 4 color categories based on the data I get: red, blue, green and yellow (respectively). I display the count for each of them on 4 <divs>
. Each div is its own ActionLink and right now sends a string to a Controller containing the color name.
The Controller then takes the string and compares it against a query where it returns the list objects that belong to that color category; then I do whatever I want to that list.
But my issue is that I am forced to do this data pull over and over and it takes too long as is. Is there a way for me to just load up the list once then pass it to my controller so I am not stuck waiting for my query to finish loading?
Here is my Model:
using System.Collections.Generic;
namespace Foo.Models
{
public class FooViewModel
{
public List<Foo> FooCollection = new List<Foo>();
/*Contains two properties
string CarName {get; set;}
string Color {get; set;}
List<Features> Features = new List<Features>();
*/
}
}
My View
@model Foo.Models.FooViewModel
@{
var RedCars = Model.FooCollection.Where(c => c.Color == "Red").ToList();
... //{yellow, blue, green}
}
<div id="FooCollection">
<section class="no-padding-top no-padding-bottom">
<div class="container-fluid">
<div class="public-user-block block">
<div class="row d-flex align-items-center">
<!--Red Cars-->
@using (Ajax.BeginForm("../Bar/Index/Red", null,
new AjaxOptions
{
HttpMethod = "post",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "CarsList"
}, new { id = "RedCarsForm" }))
{
<input type="hidden" name="Cars" value="@RedCars" />
<div id="status-container" class="col-lg-3 d-flex align-items-center">
<button type="submit">@RedAlerts.Count</button>
<strong>Red Cars</strong>
</div>
}
<!-- same structure for yellow, green, blue --!>
</section>
</div>
My Controller:
public ActionResult Index()
{
foreach (var car in db.database.Db_GetCars())
{
model.FooCollection.Add(new Foo()
{
CarName = car.CarName,
Color= car.Color
});
}
return View(model);
}
Destination Controller:
namespace Foo.Controllers
{
public class BarController: Controller
{
BarViewModel model = new BarViewModel();
[HttpPost, Route("/Bar/Index/{color}")]
public ActionResult Index(List<Foo> Cars)
{
//logic goes here
return View(model);
}
}
}
My desired output is to send the complete List<Foo>
object to my Destination Controller. But right now I am getting a value count of zero (0) when it gets to the controller. Can anyone see what I am doing wrong? Many thanks in advance.