Home Send list from view to cintroller asp.net mvc.
Reply: 0

Send list from view to cintroller asp.net mvc.

Atelj Luka
1#
Atelj Luka Published in 2017-12-04 09:04:32Z

Why is kosarasVM.kosare.count == 0 View Works great but when i send the model back to controller i can't get the list from the model. I am new to MVC so sorry if this is the basic question

this is my model

 public class UserKosarasVM
    {
        public UserKosarasVM()
        {
            Kosare = new List<KosaraVM>();
        }
        public List<KosaraVM> Kosare;
    }

this is my view

@using (Html.BeginForm("Izracunaj", "Kosaras", Model))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        @foreach (var item in Model.Kosare)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.ArtiklVM.Naziv)
                </td>
                <td>
                    <div class="form-group">
                        <div class="col-md-10">
                            @Html.EditorFor(model => item.Kolicina, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => item.Kolicina, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ArtiklVM.Cjena)
                </td>
    </tr>

        }
               <td>

            <button type="submit" class="btn btn-primary">Izracunaj</button>
        </td>       
    </div>
}
  • this is my controller

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Izracunaj([Bind(Include = "Kosare")] UserKosarasVM kosarasVM)
    {
    
            decimal ukupnaCjena = 0;
            foreach (var kosaraVM in kosarasVM.Kosare)
            {
                ukupnaCjena += kosaraVM.ArtiklVM.Cjena;
            }
    
            ViewBag.ukupnaCjena = ukupnaCjena;
    
            return View("Index", kosarasVM);
    }
    

enter code here

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315175 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO