0

I create a table in my CSHTML, I want to pass the array of nr of items == aantal back to my controller however this doesn't seem to work. Any idea whats wrong or why I get a null reference in my controller?

CSHTML

@using (Html.BeginForm("OrderConfirm", "Beurs", new { vm = Model.Aantal }, method: FormMethod.Post))
    {
<table class="table table-striped table-condensed table-bordered">
    <tr>
        <th>
            Naam
        </th>
        <th>
            Prijs
        </th>
        <th>
            Minimum prijs
        </th>
        <th>
            Factor
        </th>
        <th> Actie</th>
        <!--
        <th>Edit</th>-->
    </tr>


        @foreach (var item in Model.ItemLijstVm)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Naam)
                </td>
                <td>
                    € @Html.DisplayFor(modelItem => item.Prijs)
                </td>
                <td>
                    € @Html.DisplayFor(modelItem => item.MinimumPrijs)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Factor)
                </td>
                <td>

                        @Html.TextBoxFor(m => m.Aantal[item.Id - 1], new {type = "number" })

                </td>   
            </tr>
        }

</table>
 <input type="submit" value=" Bevestig bestelling " width="120" />
}

ViewModel

public class BeursLijstViewModel
{
public IEnumerable<BeursItemViewModel> ItemLijstVm{get; set;}

public string Naam { get; set; }

public double Crash { get; set; }

//References naar animated gif
public bool  Event { get; set; }
public string GifPath { get; set; }


public int[] Aantal { get; set; }

public int VerhoogAllePrijzen { get; set; }

public double Totaal { get; set; }

public SelectListItem Categorie { get; set; }

public BeursLijstViewModel(Beurs beurs)
{

    ItemLijstVm= beurs.Items.Select(g => new BeursItemViewModel(g));
    Naam = beurs.Naam;
    Aantal = new int[beurs.Items.Count()];
    Totaal = beurs.Totaal;
}



}

Controller

[HttpPost]
        public ActionResult OrderConfirm(int[] vm) //VM is null but should be array
        {
           //Some more code



        }

The reference I get on my post from my model is null, but if i declare it in my foreach loop like this, it works. I really don't have a clue what goes wrong:

 @using (Html.BeginForm("Add", "Beurs", new { id = item.Id, aantal = Model.Aantal }, method: FormMethod.Post))
                    {
                        @Html.TextBoxFor(m => m.Aantal[item.Id - 1], new { type = "number" })
                        <input type="submit" value=" + " width="120"/>
                    }
Ruben Guillemyn
  • 107
  • 1
  • 9
  • A simple way is to take all necessary data in an object and call the controller action method from jquery/angularjs – Rakesh Burbure Oct 28 '17 at 18:12
  • When you do `Html.BeginForm("OrderConfirm", "Beurs", new { vm = Model.Aantal },` you are saying you're passing `vm` as a query string parameter. Is `Model.Aantal` property encoded? If you do not intend to pass this through the URL then you'll need to review how to properly pass a collection through the form body. And the browser's network monitor is a big help in debugging these types of problems. – Jasen Oct 28 '17 at 19:25
  • @RakeshBurbure: I kinda have the lack of knowledge on jquery/angular – Ruben Guillemyn Oct 30 '17 at 14:36
  • @Jasen my URL is : http://localhost/Beurs/OrderConfirm?vm=BeursFuif.BeursLijstViewModel – Ruben Guillemyn Oct 30 '17 at 14:49
  • You probably don't want to pass the collection through the URL. See [this](https://stackoverflow.com/questions/19964553/mvc-form-not-able-to-post-list-of-objects) question and [this](https://stackoverflow.com/questions/16321736/how-can-i-post-a-list-of-items-in-mvc) to see how to post collections. – Jasen Oct 30 '17 at 17:21

2 Answers2

0

I think you have to pass back the actual model not just array expecting to get result, since when it binds it goes several layers like name='Model.item[z]'

        [HttpPost]
        public ActionResult OrderConfirm(BeursLijstViewModel vm) 
        {
               foreach (var item in vm.ItemLijstVm)
               {
                    var response=   vm.Aantal[item.Id - 1]
               }
        }
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • I've changed my code, however my entire model seems to be empty. Probably because I don't pass it through my URL. I'm not sure why passing an int Id and an int aantal does work? – Ruben Guillemyn Oct 30 '17 at 14:43
0
@using (Html.BeginForm("OrderConfirm", "Beurs", new { vm = Model.Aantal }, method: FormMethod.Post))

changing the above part in combination with the answer of @COLD TOLD to

@using (Html.BeginForm("OrderConfirm", "Beurs"))

fixed my problem. Thanks for the help!

Ruben Guillemyn
  • 107
  • 1
  • 9