简体   繁体   English

如何使用Jquery或Javascript将模型列表从控制器传递到视图

[英]How to pass Model List from Controller to View with Jquery or Javascript

In my order module, I have three dropdownlist for all products. 在我的订单模块中,所有产品都有三个下拉列表。

First dropdownlist for size. 大小的第一个下拉列表。 (If user not select a value, second and third dropdown not appear) (如果用户未选择值,则不会出现第二和第三下拉菜单)

Second dropdownlist for color: If user select a size from dropdownlist, second dropdown is appear. 颜色的第二个下拉列表:如果用户从下拉列表中选择尺寸,则会显示第二个下拉列表。 But I don't want to show all of colors. 但是我不想显示所有的颜色。 I want to show only the colors of the selected product size. 我只想显示所选产品尺寸的颜色。

And lastly, third dropdown list for quantity. 最后是数量的第三下拉列表。 User will select quantity and send it to cart. 用户将选择数量并将其发送到购物车。

I am beginner in jquery ajax and need for your help. 我是jquery ajax的初学者,需要您的帮助。

Here is my Model Views: 这是我的模型视图:

public class ColorForOrder
{
   public List<ColorFromVariationVM> colorlist { get; set; }
}

And second VM: 第二台虚拟机:

public class ColorFromVariationVM
    {
        public int ID { get; set; }
        public string Color { get; set; }
        public int? ColorQuantity { get; set; }
    }

My View: 我的观点:

<table id="example1" class="table table-bordered dt-responsive nowrap table-striped">
            <thead>
                <tr>
                    <th width="20%">Product Name</th>
                    <th width="30%">Size( Height - Width - Sections )</th>
                    <th width="20%">Color</th>
                    <th width="10%">Quantity</th>
                    <th width="20%">Transaction</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.ProductName</td>
                        <td>
                            <div class="form-group" style="width: 100%;">
                                @*@Html.DropDownListFor(x => item.SizeId, new SelectList(item.ProductSizeList, "ID", "SizeForOrder"),"Select Size", new { @class = "form-control select2",@style="width:100%", @placeholder = "" })
                                    <span class="text-danger">@Html.ValidationMessageFor(x => item.SizeId)</span>*@

                                <select id="select-size-@item.ID" class="form-control select2" style="width: 100%;">
                                    <option selected="selected" disabled="disabled">Size</option>
                                    @foreach (var size in item.ProductSizeList)
                                    {
                                        if (size.Quantity == null || size.Quantity == 0)
                                        {
                                            <option disabled="disabled">@size.SizeForOrder (Out of Stock)</option>
                                        }
                                        else
                                        {
                                            <option>@size.SizeForOrder</option>
                                        }

                                    }
                                </select>
                            </div>
                        </td>
                        <td>
                            <div id="color-@item.ID" class="form-group" style="width: 100%;">
                                @*<select class="form-control select2" style="width: 100%;">
                                        <option selected="selected" disabled="disabled">Color</option>
                                        <option>TXB</option>
                                        <option>TXW</option>
                                        <option>TXG</option>
                                        <option>MATT</option>
                                        <option>BRUSHED</option>
                                        <option>POLISHED</option>
                                    </select>*@
                            </div>
                        </td>
                        <td>
                            <div id="quantity-@item.ID" class="form-group" style="width: 100%;">
                                @*<select class="form-control select2" style="width: 100%;">
                                        <option selected="selected" disabled="disabled">QTY</option>
                                        <option>1</option>
                                        <option>2</option>
                                        <option>3</option>
                                        <option>4</option>
                                        <option>5</option>
                                        <option>6</option>
                                        <option>7</option>
                                    </select>*@
                            </div>
                        </td>
                        <td>
                            @*  <a href="@Url.Action("EditProduct", "Product", new { id = item.ID })" id="add-basket-@item.ID" class="btn btn-primary btn-sm" title="Edit"><i class="fa fa-fw fa-cart-plus"></i> Add to Basket</a>*@
                            @*<a href="@Url.Action("VariationList", "Variation", new { id = item.ID, p=item.ProductName })" class="btn btn-primary btn-xs" title="Variation"><i class="icon-sitemap"></i></a>*@
                            @* <a href="@Url.Action("Delete","Product", new { id = item.ID })" onclick="return confirm('Are you sure you want to delete this product with all variations?')" class="btn btn-danger btn-xs" title="Delete"><i class="icon-remove"></i></a>*@
                        </td>

                    </tr>
                }

            </tbody>
            <tfoot>
                <tr>
                    <th>Product Name</th>
                    <th>Size (Height-Width-Sections)</th>
                    <th>Color</th>
                    <th>Quantity</th>
                    <th>Transaction</th>
                </tr>
            </tfoot>
        </table>

My Script: 我的剧本:

<script>

    $(document).ready(function () {

        @foreach (var item in Model)
        {
          <text>
        $('select#select-size-@(item.ID)').change(function() {

            var id = $(this).data("id");
            var link = "/Order/GetColorFromSize/" + @item.ID;
            var a = $(this);
            $.ajax({
                type: "GET",
                url: link,
                success: function(result) {

                    $("div#color-@(item.ID)").html('<select class="form-control select2" style="width: 100%;">' + 
                        $.each(result,function(index,value)
                        { 
                            $.each(value[0],function(indexx,valuee)
                            { 
                                '<option>'+valuee[1]+'</option>'
                            }) 
                        }) 
                        + '</select>');
                }
            });
        });
        </text>
            }
        });
</script>

And lastly my controller: 最后是我的控制器:

public List<Models.DTO.OrderDTO.ColorForOrder> GetColorFromSize(int id)
        {
            List<Models.DTO.OrderDTO.ColorForOrder> colormodel = rpvariation.Where(x => x.Id == id).Select(a => new Models.DTO.OrderDTO.ColorForOrder()
            {
                colorlist = a.OwnerProduct.Colors.Select(b => new Models.DTO.OrderDTO.ColorFromVariationVM()
                {
                    ID = b.Id,
                    Color = b.Color.ColorName,
                    ColorQuantity = b.Product.Variations.Sum(x => x.Stock.TXB)
                }).ToList()
            }).ToList();

            return colormodel;

        }
public ActionResult GetColorFromSize(int id)
{
    var colormodel = rpvariation
        .Where(x => x.Id == id)
        .Select(a => new Models.DTO.OrderDTO.ColorForOrder()
        {
            colorlist = a.OwnerProduct.Colors.Select(b => new Models.DTO.OrderDTO.ColorFromVariationVM()
            {
                ID = b.Id,
                Color = b.Color.ColorName,
                ColorQuantity = b.Product.Variations.Sum(x => x.Stock.TXB)
            }).ToList()
        }).ToList();

    return Json(colormodel, JsonRequestBehaviour.AllowGet);
}

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

相关问题 如何将整个模型从视图传递到控制器或Javascript - how to pass entire model from view to controller or Javascript 如何将模型从视图传递到控制器,将项目添加到列表,传递回视图 - How to pass Model from View to Controller, add items to list, pass back to view 如何将JavaScript值从View传递到Controller? - How to pass JavaScript value from View to Controller? 如何从 controller 传递列表以查看和显示 - How to pass list from controller to view and display 在MVC 5中,如何将表对象数据作为列表传递给Controller View Model? - In MVC 5 how to pass table Object data as a list to Controller View Model? 如何从控制器JavaScript传递数据以在AngularJS中查看JavaScript? - How to pass data from controller javascript to view javascript in AngularJS? 如何使用Ajax将模型从视图传递到控制器 - How to pass model from view to controller using ajax 如何将模型值 IFormFile 从视图传递到控制器 On Change 事件 - How to pass model value IFormFile from view to controller On Change event 单击MVC中的按钮时如何将模型从视图传递到控制器? - How to pass model from view to controller when click button in MVC? 如何通过用JavaScript编写的确认对话框将模型传递给控制器 - How to pass Model to a Controller from a confirm Dialog written with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM