在通过级联加载的selectchange上将项目添加到c#列表中 [英] add item to c# list on selectchange loaded through cascading

查看:88
本文介绍了在通过级联加载的selectchange上将项目添加到c#列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个attr的selectList,它是通过级联填充的(在选择一个公司时,我只显示所选的公司模型)现在我想允许用户选择多个模型,以便在选择时,项目应添加到c#列表中并显示在页面,然后允许用户选择其他任何一家公司中的更多公司.以下是我的代码. OrderViewModel

i have selectList with multiple attr which is filled through cascading(on select a company i just show selected Company Models) Now i want to allow user to select multiple models such that on select, item should add in c# list and display in page and then allow user to select more of any other company.Picture attached Following is my code. OrderViewModel

      public class OrderViewModel
      {
        [Display(Name ="Order ID")]
        public int order_id { get; set; }
        [Required]

        public string cus_name { get; set; }
        public string cus_phone { get; set; } 
        public System.DateTime Date { get; set; }
        [DataType(DataType.Date)]
        public System.DateTime Date { get; set; }
        public int Amount { get; set; }
        public List<Products> Products { get; set; }

      }

我想绑定OrderViewModel的产品"列表中的选定商品,该商品将与其他"字段一起发送到服务器. 产品

i want to bind selected Item in 'Products' List of OrderViewModel which will be send to server with Further fields. Products

public class Products
{
    public int id { get; set; }
    public int modelId { get; set; }
    public int Phoneid { get; set; }
    public int Quantity { get; set; }
    public double price { get; set; }
    public bool isSelected { get; set; }
    public int order_id { get; set; }
}

剃刀视图

 <div class="form-group row">
                <label  class="control-label col-6">Company Name</label>
                <div class="col-12">
                    <select id="CompanyId"  class="custom-select mr-sm-2"
                            asp-items="@(new SelectList( @ViewBag.Companies,"Phoneid","Com_name"))">
                        <option value="">Please Select</option>
                    </select>
                </div>
                <span  class="text-danger"></span>
            </div>
            <div class="form-group row">
                <label  class="control-label col-6"></label>
                <div class="col-12">
                    <select id="modelId" multiple class="custom-select mr-sm-2"
                            asp-items="@(new SelectList(string.Empty,"modelId","model_name","--Select--"))">
                        <option value="">Please Select</option>
                    </select>
                </div>
                <span class="text-danger"></span>
            </div>

我还没有尝试在列表中添加项目

what i have tried yet to add item in list

 <script>
                $("#modelId").change(function () {
                    var list = @(Model.Products);
                    let item = $(this).children("option:selected").val();

                    list.forEach(x => {
                        if (x.modelId != item) {

                            @{ 

                                Products products = new Products()
                                {
                                    isSelected=true,
                                    modelId= item,
                                };
                                Model.Products.Add(products);
                            }
                        }
                    });
                })
                 @for (int i = 0; i < Model.Products.Count; i++)
                 {

                 }
            </script>

我现在要通过局部视图显示所有选定的产品,我只想将这些选定的产品以及每个产品的数量和价格发送给服务器

推荐答案

下面是一个工作示例,如下所示:

Here is a working demo like below:

型号:

public class Model
{
    [Key]
    public int modelId { get; set; }
    [Display(Name = "Model Name")]
    public string model_name { get; set; }
    public int Phoneid { get; set; }
    public IList<Products> Products { get; set; }
}
public class Company
{
    [Key]
    public int Phoneid { get; set; }
    [Display(Name = "Company Name")]
    public string Com_name { get; set; }
}
public class Products
{
    public int id { get; set; }
    public int modelId { get; set; }
    public int Phoneid { get; set; }
    public int Quantity { get; set; }
    public double price { get; set; }
    public bool isSelected { get; set; }
    public int order_id { get; set; }
}

查看(Index.cshtml):

View(Index.cshtml):

@model Products
<div>
    <div style="float:left;width:40%">
        <form id="form">
            <div class="form-group row">
                <label>Company Name</label>
                <div class="col-12">
                    <select id="CompanyId" asp-for="Phoneid" class="custom-select mr-sm-2"
                            asp-items="@(new SelectList( @ViewBag.Companies,"Phoneid","Com_name"))">
                        <option value="">Please Select</option>
                    </select>
                </div>
            </div>
            <div class="form-group row">
                <label>Model Name</label>
                <div class="col-12">
                    <select id="modelId" multiple class="custom-select mr-sm-2" name="modelId"
                            asp-items="@(new SelectList(string.Empty,"modelId","model_name","--Select--"))">
                        <option value="">Please Select</option>
                    </select>
                </div>                
            </div>
            <div>
                <input type="button" id="saveBtn" value="Save" />
            </div>
        </form>

    </div>
    <div style="float:right;width:60%">
        <h5>Products</h5>
        <div id="products"></div>
    </div>
</div>

@section Scripts
{
    <script>
    $(function () {
        $('#CompanyId').change(function () {  
            var data = $("#CompanyId").val();
            console.log(data);
            $.ajax({
                url: '/Home/GetModel?Phoneid=' + $("#CompanyId").val(),
                type: 'Get',
                success: function (data) { 
                    var items = "<option value='0'>Select</option>";
                    $.each(data, function (i, item) {
                        items += "<option value='" + item.value + "'>" + item.text + "</option>";
                    });
                    $('#modelId').html(items);

                }
            })
        });
        $('#saveBtn').click(function () {
            $.ajax({
                url: '/Home/GetProduct?Phoneid=' + $("#CompanyId").val() + "&modelId=" + $('#modelId').val(),
                type: 'Post',
                success: function (data) {
                    $('#products').html(data);
                }
            })
        })                   
    })

    </script>
}

部分视图(_Partial.cshtml):

Partial View(_Partial.cshtml):

@model IEnumerable<Products>
<table class="table">
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>check</td>
                <td>
                    <input asp-for="@item.isSelected" />
                </td>
                <td>Product Id</td>
                <td>
                    @Html.DisplayFor(modelItem => item.id)
                </td>
            </tr>
            <tr>
                <td>Quantity</td>
                <td>
                    @Html.DisplayFor(modelItem => item.Quantity)
                </td>
                <td>Price</td>
                <td>
                    @Html.DisplayFor(modelItem => item.price)
                </td>
            </tr>
        }
    </tbody>
</table>

控制器:

public class HomeController : Controller
{
    private readonly MvcProj3Context _context;
    public HomeController(MvcProj3Context context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        ViewBag.Companies = _context.Company.ToList();
        return View();
    }
    public JsonResult GetModel(int Phoneid)
    {
        List<Model> model = new List<Model>();
        model = (from m in _context.Model
                 where m.Phoneid == Phoneid
                 select m).ToList();
        return Json(new SelectList(model, "modelId", "model_name"));
    }
    [HttpPost]
    public IActionResult GetProduct(int Phoneid, string[] modelId)
    {
        var data = new List<Products>();
        var ids = modelId[0].Split(',');
        foreach(var item in ids)
        {
            var id = int.Parse(item);
            //guess the modelA in CompanyA contains several products
            var product = (from p in _context.Products
                            where p.Phoneid == Phoneid && p.modelId == id
                            select p).ToList();
            foreach (var pro in product)
            {
                data.Add(pro);
            }
        }
        return PartialView("_Partial", data);
    }
}

结果:

这篇关于在通过级联加载的selectchange上将项目添加到c#列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆