【问题标题】:HTM BeginCollectionItem returns only first item of collectionHTM BeginCollectionItem 仅返回集合的第一项
【发布时间】:2021-02-13 21:49:49
【问题描述】:

我有以下问题:

“礼物”的集合作为参数传递给视图。 此集合中的每个项目都是模型类“Gift”中的一个对象。 这个类中只有两个属性:名称和价格。 为了简单起见,我将类放在控制器中。

这是动作方法:

// 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);
    }

感谢 BeginCollectionItem Html 帮助器,在视图中我可以动态地将新项目添加到集合中。

这是我的观点:

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

这是名为“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>

}

在我的控制器中,我也有这个动作方法:

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

只有集合中已经存在的第一项,才会传递给我的控制器中的 HTTP Post 方法。

【问题讨论】:

    标签: c# asp.net asp.net-mvc-5 begincollectionitem


    【解决方案1】:

    我发现了我的错误。 部分视图“GiftEditorRow”仍然包含以下内容:

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

    如果你删除它,它应该可以工作。

    这是我现在的部分观点:

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

    现在我得到了所有项目,而不仅仅是第一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多