如何从 MVC 项目中的每个表行中获取每个值
本文关键字:获取 MVC 项目 | 更新日期: 2023-09-27 18:33:16
我的观点中有以下网络网格:
@using (Html.BeginForm("InsertSensorsInExistingPredefineView", "PredefinedViews", FormMethod.Post))
{
@gridSensorChoose.GetHtml(
tableStyle: "webgrid3-table",
headerStyle: "webgrid3-header",
footerStyle: "webgrid3-footer",
alternatingRowStyle: "webgrid3-alternating-row",//
selectedRowStyle: "webgrid3-selected-row",
rowStyle: "webgrid3-row-style",//
mode: WebGridPagerModes.All,
columns:
gridSensorChoose.Columns(
gridSensorChoose.Column("SensorID", format: @<text> <input readonly="readonly" name="SensorID" id="SensorID" value="@item.SensorID" /> </text>, style: "col1Width"),
gridSensorChoose.Column("DeviceID", format: @<text> <span id="DeviceID">@item.DeviceID</span> </text>, style: "col1Width"),
gridSensorChoose.Column("SensorType", format: @<text> <span id="SensorType" class="SensorType">@item.SensorType</span> </text>, style: "col1Width"),
gridSensorChoose.Column("SensorName", @Html.Localize("sensorName").ToString(), format: @<text> <span><label id="SensorName">@item.SensorName</label> </span> </text>, style: "col2Width", canSort: false),
gridSensorChoose.Column("VAL", format: @<text><span class="Val"> @Html.CheckBox("val") </span></text>, style:"col1Width", canSort: false),
gridSensorChoose.Column("MIN", format: @<text><span class="Min"> @Html.CheckBox("min") </span> </text>, style: "col1Width", canSort: false),
gridSensorChoose.Column("AVG", format: @<text> <span class="Avg"> @Html.CheckBox("avg") </span> </text>, style: "col1Width", canSort: false),
gridSensorChoose.Column("MAX", format: @<text> <span class="Max"> @Html.CheckBox("max") </span> </text>, style: "col1Width", canSort: false),
gridSensorChoose.Column("SUM", format: @<text> <span class="Sum"> @Html.CheckBox("sum") </span> </text>, style: "col1Width", canSort: false),
gridSensorChoose.Column("INT", format: @<text> <span class="Int"> @Html.CheckBox("int") </span> </text>, style: "col1Width", canSort: false)
))
<input type="submit" value="@Html.Localize("save")" formaction="/PredefinedViews/InsertSensorsInExistingPredefineView" />
}
此视图在浏览器中显示以下 html,如下所示:
<table class="webgrid3-table">
<thead>
<tr class="webgrid3-header">
<th scope="col" style="display: none;">
<a href="/PredefinedViews/CreateSensorList? selDeviceID=1&sort=SensorID&sortdir=ASC">SensorID</a>
</th>
<th scope="col" style="display: none;">
<a href="/PredefinedViews/CreateSensorList?selDeviceID=1&sort=DeviceID&sortdir=ASC">DeviceID</a>
</th>
<th scope="col" style="display: none;">
<a href="/PredefinedViews/CreateSensorList?selDeviceID=1&sort=SensorType&sortdir=ASC">SensorType</a>
</th>
<th scope="col">
Sensor Name
</th>
<th scope="col">
VAL
</th>
<th scope="col">
MIN
</th>
<th scope="col">
AVG
</th>
<th scope="col">
MAX
</th>
<th scope="col">
SUM
</th>
<th scope="col">
INT
</th>
</tr>
</thead>
<tbody>
<tr class="webgrid3-row-style">
<td class="col1Width" style="display: none;"> <span id="SensorID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">10</span> </td>
<td class="col2Width"> <span><label id="SensorName">Soil Temperature </label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int" style="display: none;"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-alternating-row">
<td class="col1Width" style="display: none;"> <span id="SensorID">2</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">1</span> </td>
<td class="col2Width"> <span><label id="SensorName">Soil Moisture na 15cm</label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int" style="display: none;"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-row-style">
<td class="col1Width" style="display: none;"> <span id="SensorID">3</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">1</span> </td>
<td class="col2Width"> <span><label id="SensorName">Soil Moisture na 30cm</label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int" style="display: none;"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-alternating-row">
<td class="col1Width" style="display: none;"> <span id="SensorID">4</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">101</span> </td>
<td class="col2Width"> <span><label id="SensorName">Leaf wetness prvi</label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min" style="display: none;"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg" style="display: none;"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max" style="display: none;"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int" style="display: none;"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-row-style">
<td class="col1Width" style="display: none;"> <span id="SensorID">5</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">201</span> </td>
<td class="col2Width"> <span><label id="SensorName">Ait temperature </label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min" style="display: none;"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg" style="display: none;"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max" style="display: none;"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-alternating-row">
<td class="col1Width" style="display: none;"> <span id="SensorID">6</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">202</span> </td>
<td class="col2Width"> <span><label id="SensorName">Air humidity </label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min" style="display: none;"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg" style="display: none;"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max" style="display: none;"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-row-style">
<td class="col1Width" style="display: none;"> <span id="SensorID">7</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">203</span> </td>
<td class="col2Width"> <span><label id="SensorName">Wind speed </label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min" style="display: none;"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg" style="display: none;"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max" style="display: none;"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-alternating-row">
<td class="col1Width" style="display: none;"> <span id="SensorID">8</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">204</span> </td>
<td class="col2Width"> <span><label id="SensorName">Wind direction </label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min" style="display: none;"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg" style="display: none;"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max" style="display: none;"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-row-style">
<td class="col1Width" style="display: none;"> <span id="SensorID">9</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">205</span> </td>
<td class="col2Width"> <span><label id="SensorName">Precipitation </label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min" style="display: none;"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg" style="display: none;"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max" style="display: none;"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-alternating-row">
<td class="col1Width" style="display: none;"> <span id="SensorID">10</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">206</span> </td>
<td class="col2Width"> <span><label id="SensorName">Solar radiation </label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min" style="display: none;"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg" style="display: none;"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max" style="display: none;"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-row-style">
<td class="col1Width" style="display: none;"> <span id="SensorID">51</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">20</span> </td>
<td class="col2Width"> <span><label id="SensorName">Battery </label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum" style="display: none;"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int" style="display: none;"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
<tr class="webgrid3-alternating-row">
<td class="col1Width" style="display: none;"> <span id="SensorID">56</span> </td>
<td class="col1Width" style="display: none;"> <span id="DeviceID">1</span> </td>
<td class="col1Width" style="display: none;"> <span id="SensorType" class="SensorType">101</span> </td>
<td class="col2Width"> <span><label id="SensorName">Leaf wetness drugi</label> </span> </td>
<td class="col1Width"><span class="Val"> <input id="val" name="val" type="checkbox" value="true"><input name="val" type="hidden" value="false"> </span></td>
<td class="col1Width"><span class="Min" style="display: none;"> <input id="min" name="min" type="checkbox" value="true"><input name="min" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Avg" style="display: none;"> <input id="avg" name="avg" type="checkbox" value="true"><input name="avg" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Max" style="display: none;"> <input id="max" name="max" type="checkbox" value="true"><input name="max" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Sum"> <input id="sum" name="sum" type="checkbox" value="true"><input name="sum" type="hidden" value="false"> </span> </td>
<td class="col1Width"> <span class="Int" style="display: none;"> <input id="int" name="int" type="checkbox" value="true"><input name="int" type="hidden" value="false"> </span> </td>
</tr>
</tbody>
所以我现在我想获取每行的每个复选框(总共有六个复选框(的每个值(即使它是假的((可以是从零到任何数字,具体取决于设备ID中有多少个SensorID(这是唯一的((每行相同((。但是我不知道如何遍历每一行。我使用 FormCollection,但这对我来说不起作用,有人有任何其他可能对我有帮助的建议吗?
public ActionResult InsertSensorsInExistingPredefineView(FormCollection sensorCollection)
{
foreach (var row in sensorCollection.AllKeys)
bool _val = sensorCollection["val"] != "false";
bool _min = sensorCollection["min"] != "false";
bool _avg = sensorCollection["avg"] != "false";
bool _max = sensorCollection["max"] != "false";
bool _sum = sensorCollection["sum"] != "false";
bool _int = sensorCollection["int"] != "false";
if (//some logic checking for at least one true value)
{
//save to some db table acording to sensorID....
}
}
return view;
}
我做了整个 != "false" 的事情,因为复选框以字符串的形式返回值,例如如果选中则为"true,false"形式的字符串,如果未选中则为"false"。这里一定是我做错了什么,我刚刚编辑了我的第一行(SensorID(,认为它可以作为计数器服务器,但我仍在挣扎。
我不知道在这里该怎么做,我有机会使用 Javascript/jQuery 完成我的任务,然后将结果发送到控制器吗?
我刚刚尝试了这个,看看它是否会遍历每一行并选取值:
<input type="submit" class="DoSomething" value="Do it" />
foreach (WebGridRow row in gridSensorChoose.Rows)
{
<script type="text/javascript">
$(function () {
$(".DoSomething").on("click", function () {
var tr = $(this).parents('tr:first');
var sensorID = tr.find(".SensorID").html();
var _val = tr.find(".Val").html();
var _min = tr.find(".Min").html();
var _avg = tr.find(".Avg").html();
var _max = tr.find(".Max").html();
var _sum = tr.find(".Sum").html();
var _int = tr.find(".Int").html();
})
if (_val == true || _min =! true || _avg == true || _max == true || _sum == true || _int == true ||){
//ajax send all vars to controller
}
//and now iterate through next row
})
</script>
也许我可以在java脚本中写出类似的东西,并在每次结束迭代时将其发送到控制器?然后它开始一个新的,直到没有更多的行。
通过你的代码行,我看到你误解了一些东西
foreach (var row in sensorCollection.AllKeys)
{
}
传感器集合中的值不是行。它们是从视图传递的字段。
您的 HTML 有很多同名的输入。举个例子:
<input id="val" name="val" type="checkbox" value="true">
sensorCollection.AllKeys
将包含"val"的多个条目 - 网格上的每一行一个条目。您正在循环访问每个值,然后忽略该值并再次检索该值。从本质上讲,这
foreach (var row in sensorCollection.AllKeys)
{
bool _val = sensorCollection["val"] != "false";
bool _min = sensorCollection["min"] != "false";
bool _avg = sensorCollection["avg"] != "false";
bool _max = sensorCollection["max"] != "false";
bool _sum = sensorCollection["sum"] != "false";
bool _int = sensorCollection["int"] != "false";
}
相当于这个:
for(int i = 0; i < number_of_rows; i++)
{
bool _val = sensorCollection["val"] != "false";
bool _min = sensorCollection["min"] != "false";
bool _avg = sensorCollection["avg"] != "false";
bool _max = sensorCollection["max"] != "false";
bool _sum = sensorCollection["sum"] != "false";
bool _int = sensorCollection["int"] != "false";
}
看看那里i
无关紧要吗?row
也是如此。您没有引用它,因此这些值将始终相同 - 可能是网格上第一行的值。
你需要做这样的事情
// find the number of values for "val" in the collection
// build a for loop that iterates over the collection
for(int i = 0; i < sensorCollection.AllKeys.Where(k => k == "val").Count(); i++)
{
// get the nth of each key, where n is the instance of "val"
bool _val = sensorCollection.AllKeys.Where(k => k == "val")[i] != "false";
// handle the other values (I'm too lazy to type them out)
// save out the data
}
更新
好的,所以我构建了一个新的 MVC 应用程序并复制了您的网格和回发,这就是我发现的。
首先,sensorCollection.AllKeys 中有 6 个值 - 每列一个值(val,min,avg,max,sum,int(
其次,由于某种原因,数据被编码为串联字符串。因此,如果您勾选第一个 val 复选框,则sensorCollection[0] = "true,false,false,false,false,false,false,false,false,false,false,false,false"
我没想到会这样,这有点奇怪,但我们会处理它。这段代码应该对你进行排序:
public ActionResult InsertSensorsInExistingPredefineView(FormCollection sensorCollection)
{
bool[] vals = sensorCollection["val"].Split(',').Select(x => Convert.ToBoolean(x)).ToArray();
bool[] mins = sensorCollection["min"].Split(',').Select(x => Convert.ToBoolean(x)).ToArray();
bool[] avgs = sensorCollection["avg"].Split(',').Select(x => Convert.ToBoolean(x)).ToArray();
bool[] maxs = sensorCollection["max"].Split(',').Select(x => Convert.ToBoolean(x)).ToArray();
bool[] sums = sensorCollection["sum"].Split(',').Select(x => Convert.ToBoolean(x)).ToArray();
bool[] ints = sensorCollection["int"].Split(',').Select(x => Convert.ToBoolean(x)).ToArray();
for (int i = 0; i < vals.Count(); i++)
{
// your first row, "Soil Temperature":
bool val = vals[i];
bool min = mins[i];
// etc
}
}
好的,我在这里得到了答案,在 roryok 的帮助下,他以正确的方式引导我:
[HttpPost]
public ActionResult InsertSensorsInExistingPredefineView(FormCollection sensorCollection)
{
int[] sensorIds = sensorCollection["SensorID"].Split(',').Select(x => Convert.ToInt32(x)).ToArray();
List<bool> vals = sensorCollection["val"].Split(',').Select(x => Convert.ToBoolean(x)).ToList();
List<bool> mins = sensorCollection["min"].Split(',').Select(x => Convert.ToBoolean(x)).ToList();
List<bool> avgs = sensorCollection["avg"].Split(',').Select(x => Convert.ToBoolean(x)).ToList();
List<bool> maxs = sensorCollection["max"].Split(',').Select(x => Convert.ToBoolean(x)).ToList();
List<bool> sums = sensorCollection["sum"].Split(',').Select(x => Convert.ToBoolean(x)).ToList();
List<bool> ints = sensorCollection["_int"].Split(',').Select(x => Convert.ToBoolean(x)).ToList();
List<bool> adjustVals = new List<bool>();
adjustVals.Add(vals[0]);
adjustVals = RemoveFalseAfterTrue(vals, adjustVals);
List<bool> adjustMins = new List<bool>();
adjustMins.Add(mins[0]);
adjustMins = RemoveFalseAfterTrue(mins, adjustMins);
List<bool> adjustAvgs = new List<bool>();
adjustAvgs.Add(avgs[0]);
adjustAvgs = RemoveFalseAfterTrue(avgs, adjustAvgs);
List<bool> adjustMaxs = new List<bool>();
adjustMaxs.Add(maxs[0]);
adjustMaxs = RemoveFalseAfterTrue(maxs, adjustMaxs);
List<bool> adjustSums = new List<bool>();
adjustSums.Add(sums[0]);
adjustSums = RemoveFalseAfterTrue(sums, adjustSums);
List<bool> adjustInts = new List<bool>();
adjustInts.Add(ints[0]);
adjustInts = RemoveFalseAfterTrue(ints, adjustInts);
for (int i = 0; i < sensorIds.Count(); i++)
{
bool val = adjustVals[i];
bool min = adjustMins[i];
bool avg = adjustAvgs[i];
bool max = adjustMaxs[i];
bool sum = adjustSums[i];
bool _int = adjustInts[i];
}
return null;
}
private static List<bool> RemoveFalseAfterTrue(List<bool> initialCheckboxValues, List<bool> adjustedInitialCheckboxValues)
{
for (int i = 1; i < initialCheckboxValues.Count; ++i)
{
if (initialCheckboxValues[i - 1] == false)
{
adjustedInitialCheckboxValues.Add(initialCheckboxValues[i]);
}
}
return adjustedInitialCheckboxValues;
}
因此,在获得初始值后,我必须将它们放在列表中,然后在每个 true 之后删除每个 false(使用我创建的方法(,现在我有了我想要的结果。