将数据从for循环视图中传递回控制器

时间:2019-02-09 18:05:17

标签: c# razor model-view-controller

我正在Razor的for循环中创建一个表单。

@model SignAPI.ViewModels.ChannelManagerViewModel

@{
    ViewBag.Title = "Index";
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>
<div class="form-horizontal">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @using (Html.BeginForm("Index", "ChannelManager", FormMethod.Post))
    {
        <div class="form-group">
            @Html.LabelFor(m => m.CustomerNames, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(x => x.CustomerNameSelected, Model.CustomerNames, "Select", new { onchange = "this.form.submit();", @class = "form-control form-control-sm", @id = "CustomerDdl", @data_val = false })
                @Html.ValidationMessageFor(m => m.CustomerNameSelected, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Devices, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(x => x.DeviceSelected, Model.Devices, "Select", new { onchange = "this.form.submit();", @class = "form -control form-control-sm", @id = "DeviceDdl", @data_val = false })
                @Html.ValidationMessageFor(m => m.DeviceSelected, "", new { @class = "text-danger" })
            </div>
        </div>
    }

    @using (Html.BeginForm("SaveData", "ChannelManager", FormMethod.Post))
    {
        <table class="table table-striped" style="font-size:small;">
            <thead>
                <tr>
                    <th>
                        Channel Name
                    </th>
                    <th>
                        UOM's'
                    </th>
                    <th>
                        Channel UOM
                    </th>
                    <th>
                        Calculated
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>

                @{  var channelNames = Model.ChannelNames.Select(x => x.ChannelName).OrderByDescending(x => Model.ChannelName).ToList();
                    for (int count = 0; count < channelNames.Count(); count++)
                    {
                        var channel = channelNames[count];

                        var ddlId = ("ddlId" + count).ToString();
                        var txtBoxId = ("txtBoxId" + count).ToString();
                        <tr>
                            @Html.HiddenFor(x => x.CustomerNameSelected)
                            @Html.HiddenFor(x => x.DeviceSelected)
                            <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(x => x.ChannelDataToSave, Model.UomSelected, new { htmlAttributes = new { @id = @txtBoxId, @class = "form-control form-control-sm" } })
                                @Html.ValidationMessageFor(x => x.ChannelType, "", new { @class = "text-danger" })
                            </td>
                            <td>
                                @Html.CheckBoxFor(x => x.Calculated)
                            </td>
                        </tr>
                    }
                }
            </tbody>
        </table>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-sm btn-primary" />
            </div>
        </div>
    }

</div>

我想使用ViewModel将第二种形式创建的所有数据传递回我的Action

 public class ChannelManagerViewModel
    {
        [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; }
        public List<ChannelManagerViewModel> ChannelDataToSave { 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; }
    }

然后我想保存数据。

我已经尝试了很多方法,包括使用局部视图,但这是行不通的。我也尝试添加循环索引

  @Html.EditorFor(x => x[count].ChannelDataToSave, Model.UomSelected, new { htmlAttributes = new { @id = @txtBoxId, @class = "form-control form-control-sm" } }) 

但这只是告诉我Cannot apply indexing with [] to an expression of type 'ChannelManagerViewModel'

我希望表单会创建一个List<ChannelManagerViewModel>并传递回Action

[HttpPost]
        public ActionResult SaveData(ChannelManagerViewModel vm)
        {
            //Do some stuff
            return View(vm);            
        }

所有传回的视图模型都是空的。

您能告诉我这是怎么回事吗?

预期结果是,在for循环中创建表单时,它将创建一个列表,以传递回Action中的Controller。目前还没有...

TIA

0 个答案:

没有答案