从<select>元素C#/。NET传递值

时间:2019-10-22 15:48:33

标签: c# .net forms

我在C#/。NET项目中有一个结帐页面,该页面被配置为从产品中提取单个价格。我添加了第二个价格选项(用于单用户和多用户订阅),并允许用户使用<select>元素在它们之间进行选择。我遇到的问题是如何将所选的<option>传递给data-price="@product.Price"product.Price

var selectedPrice = 0.00m;

var totalAmount = Model.Products.Sum(x => x.Price * Model.ProductIDs.Where(productID => productID == x.ID).Count());
var discount = ConfigurationsHelper.Discount;
var finalAmount = totalAmount - discount + ConfigurationsHelper.FlatDeliveryCharges;

<table class="table table-bordered">
    <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Price</th>
            <th scope="col">Quantity</th>
            <th scope="col">Total</th>
            <th scope="col">Action</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var product in Model.Products)
        {
            i++;

            <tr>
                <th scope="row">@i</th>
                <td>
                    <a href="@Url.ProductDetails(product.Category.SanitizedName, product.ID)">
                        @product.Name
                    </a>
                </td>
                <td>
                    <select name="price" class="form-control" id="selectedPrice">
                        <option name="SingleUser" data-id="singlePrice" value="@product.Price.WithCurrency()">Single-User: @product.Price.WithCurrency()</option>
                        <option name="MultiUser" data-id="multiPrice" value="@product.MultiPrice.WithCurrency()">Multi-User: @product.MultiPrice.WithCurrency()</option>
                    </select>

                    <script>
                        $('select').on('change', function () {
                            selectedPrice = this.value;
                        });
                    </script>
                </td>

                <td>
                    <input type="number" class="form-control changeQuantity productQuantity" value="@Model.ProductIDs.Where(x => x == product.ID).Count()" data-id="@product.ID" data-price="selectedPrice" min="1" max="100" title="Quantity">
                </td>
                <td>
                    @((selectedPrice * Model.ProductIDs.Where(x => x == product.ID).Count()).WithCurrency())
                </td>
                <td>
                    <button class="deleteCartProduct btn btn-danger" type="button">
                        <i class="fas fa-times"></i>
                    </button>
                </td>
            </tr>
        }
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="text-right">
                <strong>Total</strong>
            </td>
            <td colspan="2">
                <span>
                    @totalAmount.WithCurrency()
                </span>
            </td>
        </tr>
        <tr>
            <td colspan="4" class="text-right">
                <strong>Discount</strong>
            </td>
            <td colspan="2">
                <span>
                    @discount.WithCurrency()
                </span>
            </td>
        </tr>
        <tr>
            <td colspan="4" class="text-right">
                <strong>Delivery Charges</strong>
            </td>
            <td colspan="2">
                <span>
                    @ConfigurationsHelper.FlatDeliveryCharges.WithCurrency()
                </span>
            </td>
        </tr>
        <tr>
            <td colspan="4" class="text-right">
                <strong>Final Amount</strong>
            </td>
            <td colspan="2">
                <span>
                    @finalAmount.WithCurrency()
                </span>
            </td>
        </tr>
        <tr>
            <td colspan="2"></td>
            <td colspan="4" class="text-right">
                <div class="btn-group btn-block" role="group">
                    <button data-href="@Url.Checkout()" class="update-cart-links btn btn-primary" type="button">
                        <i class="fas fa-sync-alt mr-1"></i>
                        Update
                    </button>
                    <button class="btn btn-success" id="nextDeliveryTab" type="button">
                        <i class="fas fa-angle-right mr-1"></i>
                        Next
                    </button>
                </div>
            </td>
        </tr>
    </tfoot>
</table>

}

0 个答案:

没有答案