0

This is a sister question from another question I posted (here). Many thanks to @nurdyguy and all of the people who helped me previously. However, I am having an issue where I am unable to pass a list of values to a destination controller that's in another View. My desired output is to send over the complete List object but right now I am getting a value count of zero (0) when it gets to the controller.

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);
        }
      }
    }

I spent some time digging around the best practices to route data but most places I've went have their forms set up like mine and it seems to work (for them). I even passed (as the second parameter) new { Cars = RedCars } but I still get zero (0). Is there something I am missing? Many thanks in advance!

mmangual83
  • 151
  • 1
  • 2
  • 11
  • 1
    Your form does not include any inputs relating to `Foo`. How would you expect the server to bind a collection of `Foo`? In any case, what is the point of your hidden inputs - even if you did generate the view correctly, there is no point sending back exactly the same data you just sent to the view. Its not clear what you trying to achieve. –  Feb 21 '18 at 12:19
  • @StephenMuecke what im trying to achieve is sending a modified list of Foo objects. For instance, I am trying to send a list of Foo objects containing the color "red". But I am getting an empty list in my destination controller. However, I did some research and managed to get my values using Session["key"] and that seems to fit my needs. I am, however, going to continue delving into POST and ROUTE values via Ajax because I want to do things right. – mmangual83 Feb 21 '18 at 19:23
  • But you form does not include any editable inputs so what do you mean 'modified'?. If you want to edit a collection and post it back, then refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) (and never generate your html manually) –  Feb 21 '18 at 21:25

0 Answers0