简体   繁体   English

HTM BeginCollectionItem 仅返回集合的第一项

[英]HTM BeginCollectionItem returns only first item of collection

I have the following problem:我有以下问题:

A collection of "Gifts" gets passed as a parameter to a view. “礼物”的集合作为参数传递给视图。 Each item inside this collection is a object from the Model class "Gift".此集合中的每个项目都是来自 Model 类“Gift”的对象。 There are only two properties inside this class: name and price.这个类中只有两个属性:名称和价格。 I put the class inside the controller for the sake of simplicity.为简单起见,我将类放在控制器中。

This is the action method:这是操作方法:

// GET: Order/CreateGift
    [HttpGet]
    public ActionResult CreateGift()
    {
        var initialData = new[]
        {
            new Gift{ Name = "Tricycle", Price = 69.95 },
            new Gift{ Name = "PS4 game", Price = 29.99 },
            new Gift{ Name = "Lazergun", Price = 49.99}
        };

        return View(initialData);
    }

On the view I can dynamically add new items to the collection, thanks to the BeginCollectionItem Html helper.在视图中,由于 BeginCollectionItem Html 帮助程序,我可以动态地向集合添加新项目。

This is my View:这是我的观点:

 @model IEnumerable<DemoWebShop.Controllers.Gift> <script src="~/Scripts/jquery-3.4.1.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#addItem").click(function () { $.ajax({ url: this.href, cache: false, success: function (html) { $("#editorRows").append(html); } }); return false; }); }); // end document.ready function </script> <h2>Gift List</h2> What do you want for your birthday? @using (Html.BeginForm()) { <div class="form-horizontal"> <div id="editorRows"> @foreach (var item in Model) { Html.RenderPartial("GiftEditorRow", item); } </div> @Html.ActionLink("Add another...", "BlankEditorRow", null, new { id = "addItem" }) <input type="submit" value="Finished" /> @*<div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Aanmaken" class="btn btn-default" /> </div> </div>*@ </div> }

This is the partial view called "GiftEditorRow" :这是名为“GiftEditorRow”的局部视图:

 @using HtmlHelpers.BeginCollectionItem @model DemoWebShop.Controllers.Gift @{ Layout = null; } @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="editorRow"> @using (Html.BeginCollectionItem("gifts")) { <label>Name: </label> @Html.TextBoxFor(x => x.Name); <label>Price: </label> @Html.TextBoxFor(x => x.Price, new { size = 4 }); <br /> } </div> }

Inside my controller I also have this actionmethod:在我的控制器中,我也有这个 actionmethod:

    public ViewResult BlankEditorRow()
    {
        return View("GiftEditorRow", new Gift());
    }

Only the First item that already existed inside the collection, gets passed to the HTTP Post method in my controller.只有集合中已经存在的第一个项目会被传递到我的控制器中的 HTTP Post 方法。

在此处输入图片说明

在此处输入图片说明

I found my mistake.我发现了我的错误。 The partial view "GiftEditorRow" still contains the following:局部视图“GiftEditorRow”仍然包含以下内容:

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

If you remove this, it should work.如果您删除它,它应该可以工作。

This is my partial view now:这是我现在的部分观点:

@using (Html.BeginCollectionItem("gifts"))
{
    <label>Name: </label>
    @Html.TextBoxFor(Model => Model.Name);
    <label>Price: </label>
    @Html.TextBoxFor(Model => Model.Price, new { size = 4 });
    <br />
}

Now I get all items instead of just the first one.现在我得到了所有物品,而不仅仅是第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyndicationFeed仅返回第一项 - SyndicationFeed only returns the first item 尝试获取集合中的第一个项目返回NULL - trying to get first item in collection returns NULL LINQ只返回集合中的一个项目 - LINQ only returns one item in collection 仅返回字符串数组中的第一项 - Returns only the first item in string array 我的脚本仅返回第一项 - My script returns the first item only 下拉列表仅返回第一个项目的项目文本/值 - Dropdown only returns item text/value from first item HierarchicalDataTemplate TreeView-ContainerFromItem仅返回第一个Item的TreeViewItem - HierarchicalDataTemplate TreeView - ContainerFromItem returns TreeViewItem only for the first Item MVC 5 BeginCollectionItem从页面中删除项目 - MVC 5 BeginCollectionItem remove item from the page EF Code First DbContext仅返回集合的第一个元素的封装实体 - EF Code First DbContext returns encapsulated entity only for first element of collection 为什么我的排序行为仅在列表中只有一个项目时发生,并且只有在第一次添加集合时才发生? - Why does my sort behavior fire with only one item in the list, and only the first time the collection is added to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM