[英]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.