【问题标题】:Return list from Razor View to Action从 Razor 视图返回列表到操作
【发布时间】:2019-02-05 23:15:45
【问题描述】:

我有一个Action,我在其中循环了未知数量的值。这些是与设备相关的频道,用户从下拉列表中选择设备,然后根据设备拥有的频道数创建表单,可以是 1 或 50 或介于两者之间的任何位置。

 //This is the model I use

        [Display(Name = "Select a customer")]
        public IEnumerable<SelectListItem> CustomerNames { get; set; }
        public string CustomerNameSelected { get; set; }

        [Display(Name = "Select a device")]
        public IEnumerable<SelectListItem> Devices { get; set; }
        public string DeviceSelected { get; set; }

        [Display(Name = "Channel Name")]
        public string ChannelName { get; set; }

        [Display(Name = "Channel Type")]
        public string ChannelType { get; set; }

        [Display(Name = "Calculated")]
        public bool Calculated { get; set; }

        public int ChannelCount { get; set; }

        public List<ChannelManagerModel> ChannelNames { get; set; }

        private List<string> UomList {
            get
            {
                var l = new List<string>();
                l.Add("Electricity");
                l.Add("Gas");
                l.Add("Water");
                l.Add("Heat");
                l.Add("kWh");
                l.Add("Wh");
                l.Add("kVarh");
                l.Add("Wh");
                l.Add("Volts 1");
                l.Add("Volts 2");
                l.Add("Volts 3");
                l.Add("Current 1");
                l.Add("Current 2");
                l.Add("Current 3");
                l.Add("Hz");
                l.Add("Litres");
                l.Add("Cubic Metres");
                l.Add("Cubic Feet");

                return l;
            }
        }

        public IEnumerable<SelectListItem> Uom {

            get {

                List<SelectListItem> sl = new List<SelectListItem>();
                foreach (var item in UomList)
                {
                    sl.Add(new SelectListItem() { Text = item, Value = item });
                };

                return sl;
            }

            set { }
        }
        public string UomSelected { get; set; }

由于我现在使用循环创建了新的表单元素,如何使用 ViewModel 将这些额外信息传递回操作。

   @{List<string> channelName = Model.ChannelNames.Select(x => x.ChannelName).OrderByDescending(x => Model.ChannelName).ToList();
                    int count = 0;
                    foreach (var channel in channelName)
                    {
                        count++;
                        var ddlId = ("ddlId" + count).ToString();
                        var txtBoxId = ("txtBoxId" + count).ToString();
                        <tr>
                            <td>
                               @Html.Label(@channel)
                            </td>
                            <td>
                                @Html.DropDownListFor(x => x.UomSelected, Model.Uom, "Select", new { @class = "form-control form-control-sm", @id = @ddlId, @data_val = false })
                                <script type="text/javascript">

                                        $('#@ddlId').change(function () {
                                            $('#@txtBoxId').val($(this).val());
                                        });
                                </script>
                            </td>
                            <td>
                                @Html.EditorFor(model => model.ChannelType, new { htmlAttributes = new { @id = @txtBoxId, @class = "form-control form-control-sm" } })
                                @Html.ValidationMessageFor(model => model.ChannelType, "", new { @class = "text-danger" })
                            </td>
                            <td>
                                @Html.CheckBoxFor(model => model.Calculated)
                            </td>
                        </tr>
                    }
                }

我想在我的脑海中我正在考虑创建另一个列表以传回视图,但我无法弄清楚如何去做,或者它是否是正确的方法。

这是我想要将新数据传回的控制器中的操作。

 public async Task<ActionResult> SaveData(ChannelManagerViewModel cvm)
    {
        var cm = new channelManagerModel();

        //Maybe loop through the ViewModel and add to the EF Model
        var channelManagerModel = new ChannelManagerModel();

        if (ModelState.IsValid)
        {
            db.ChannelManager.Add(channelManagerModel);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(channelManagerModel);
    }

【问题讨论】:

    标签: c# razor viewmodel


    【解决方案1】:

    前几天这样做了,在您的视图中使用 for(int count... 循环而不是 foreach,如下所示;

    for(int count=0;count<channelName.Count;count++) {
         <tr>
              <td>
                   @Html.Label(@channelName[count])
              </td>
              <td>
                   @Html.DropDownListFor(x => x[count].UomSelected, Model.Uom, "Select", new { @class = "form-control form-control-sm", @data_val = false })
              </td>
              <td>
                   @Html.EditorFor(x => x[count].ChannelType, new { htmlAttributes = new { @id = @txtBoxId, @class = "form-control form-control-sm" } })
                   @Html.ValidationMessageFor(x => x[count].ChannelType, "", new { @class = "text-danger" })
              </td>
              <td>
                   @Html.CheckBoxFor(x => x[count].Calculated)
              </td>
         </tr>
    }
    

    现在在你的控制器中你可以有类似下面的东西

    public async Task<ActionResult> SaveData(List<ChannelManagerViewModel> cvm)
    

    【讨论】:

    • 我确实尝试过,但我得到了错误“无法使用 [] 将索引应用于“ChannelManagerViewModel”类型的表达式
    • 我认为它不是一个列表。
    • 这是因为我有非列表属性。
    • 所以您要返回一个包含列表的对象?以前我已将列表推送到列表的表单部分的一部分,然后将主要对象保留在原始 cshtml 中
    猜你喜欢
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 2016-02-05
    • 2020-02-25
    • 2019-03-03
    相关资源
    最近更新 更多