我有一个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>
}
}
我想我正在考虑创建另一个List传递回视图,但是我不知道该怎么做,或者它是否是正确的方法。
这是控制器中我要将新数据传递回去的动作。
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);
}
答案 0 :(得分:-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)