在ASP.NET中的一个视图中创建多个对象

时间:2016-09-17 08:48:07

标签: c# asp.net-mvc list view

我想从一个视图创建多个对象(例如,一个视图中的5个项目)。我的观点如下:

@model IEnumerable<WerehouseProject.Models.ItemRentList>

@{
    ViewBag.Title = "Create";
}

<h2>Dodaj</h2>

@{Html.RenderPartial("_PartialError");}

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @for (int i = 0; i < Model.Count; i++)
    {
        <div class="form-group">
            @Html.LabelFor(model => model[i].ItemID, "Przedmiot", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ItemID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model[i].ItemID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model[i].Ilosc, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model[i].Ilosc, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model[i].Ilosc, "", new { @class = "text-danger" })
            </div>
        </div>

        @Html.HiddenFor(model => model[i].ItemRentID)
    }



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Dodaj" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    <button type="button" name="ID" value="@ViewBag.ItemRentID" onclick="location.href='@Url.Action("Details", "ItemRents", new { ID = @ViewBag.ItemRentID })'">Wróć</button>
</div>

创建GET方法:

public ActionResult Create(int ID, int quantity)
        {
            var model = new List<ItemRentList>();
            for(int i = 0; i < quantity; i++)
            {
                model.Add(new ItemRentList { ItemRentID = ID } );
            }
            ViewBag.ItemRentID = ID;
            ViewBag.ItemID = new SelectList(db.Items, "ItemID", "Nazwa");
            return View(model);
        }

我的POST创建方法:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "IRListID,Ilosc,ItemRentID,ItemID")] IEnumerable<ItemRentList> itemRentList)
    { 
       ... 
    }

我的 itemRentList.ItemID 总是0,有人可以告诉我如何编写View或Controller来获取POST方法中的ItemRentList列表?

这是我的ItemRentList模型

public class ItemRentList
    {
        [Key]
        public int IRListID { get; set; }

        [Display(Name = "Ilość")]
        public double Ilosc { get; set; }

        [Display(Name = "Nazwa przedmiotu")]
        public int ItemID { get; set; }
        public virtual Item Item { get; set; }

        public int ItemRentID { get; set; }
        public virtual ItemShopping ItemRent { get; set; }
    }

如果我编辑View和Controller只创建一个并且总是只创建一个ItemRentList(不是列表中的一个对象,只是简单的ItemRentList)那么ItemID的值正确绑定到object,问题是当我想创建一个列表ItemRentList,然后ItemID始终为0,不管我从DropDownList中选择什么。

0 个答案:

没有答案