从视图发送列表到控制器

时间:2017-12-04 09:04:33

标签: c# asp.net-mvc razor

为什么kosarasVM.kosare.count等于零? 视图效果很好但是当我将模型发送回控制器时,我无法从模型中获取列表。 我是MVC的新手,很抱歉,如果这是基本问题

这是我的模特:

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

这是我的观点:

@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>
}

这是我的控制者:

    [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

0 个答案:

没有答案