简体   繁体   English

HtmlBeginCollectionItem获取当前项目

[英]HtmlBeginCollectionItem Get Current Item

I need: 我需要:

  1. Acess /Client/Create 访问/客户/创建
  2. Add dynamically Partial Views (/Product/Card) and bind them to Client.Products 动态添加部分视图(/ Product / Card)并将其绑定到Client.Products
  3. In each Partial View when i click in a button open a bootstrap modal windows where i can set Product's information 在每个局部视图中,当我单击按钮时,打开一个引导模态窗口,我可以在其中设置产品信息
  4. Close the modal and reflect changes of modal reflect in the Card's Product. 关闭模态,并在卡产品中反映模态变化。

The problem is: how to change product informations in another view(other than Card) and reflect to the product of the card? 问题是:如何在其他视图(卡除外)中更改产品信息并反映到卡的产品上?

@using (Html.BeginCollectionItem("Products"))
{
    @Html.HiddenFor(model => model.ClientID)
    @Html.HiddenFor(model => model.ProductID)


    <div class="card">
        <img class="card-img-top" src="http://macbook.nl/wp-content/themes/macbook/images/png/iphone318x180.png" alt="Grupo Logo">
        <div class="card-block">
            <h4 class="card-title">@Model.Name</h4>
            <p class="card-text">@Model.Desc</p>
            <div class="btn-group">
                <button type ="button" class="btn btn-primary open-modal" data-path="/Product/Edit/@Model.ProductID">Edit</button>
                <button type="button" class="btn btn-primary open-modal" data-path="/Product/Features/@Model.ProductID">Features</button>
            </div>
        </div>
    </div>

}

在此处输入图片说明

You can do this is another view (or by dynamically loading another view into a modal. The object has not been created yet, and since you using BeginCollectionItem() to generate new items, any other view you used would not be using the same Guid created by that helper so you would not be able to match up the collection items. 您可以在另一个视图中执行此操作(或通过将另一个视图动态加载到模式中来完成。该对象尚未创建,并且由于您使用BeginCollectionItem()生成新项目,因此您使用的任何其他视图都不会使用相同的Guid由该助手创建,因此您将无法匹配收集项。

Instead, include the 'other' properties within the partial, but put them in a hidden element that gets displayed as a modal when you click the buttons. 取而代之的是,将“其他”属性包含在部分属性中,但将其放置在隐藏元素中,当您单击按钮时,该隐藏元素将以模态显示。

The basic structure of the partial for adding/editing a Product would be 用于添加/编辑产品的部分的基本结构为

<div class="product">
    @using (Html.BeginCollectionItem("Products"))
    {
        @Html.HiddenFor(m => m.ClientID)
        @Html.HiddenFor(m => m.ProductID)
        <div class="card">
            ....
            <button type ="button" class="btn btn-primary edit">Edit</button>
        </div>
        // Modal for editing additional data
        <div class="modal">
            @Html.TxtBoxFor(m => m.SomeProperty)
            @Html.TxtBoxFor(m => m.AnotherProperty)
            ....
        </div>
    }
</div>

And then handle the buttons .click() event (using delegation since the partials will be dynamically added to the view) to display the associated modal (assumes you have a <div id="products"> element that is the container for all Products) 然后处理按钮.click()事件(使用委派,因为会将部分对象动态添加到视图中)以显示关联的模式(假设您有一个<div id="products">元素,该元素是所有Products的容器)

$('#products').on('click', '.edit', function() {
    var modal = $(this).closest('.product').find('.modal');
    modal.show(); // display the modal
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM