如何从MVC项目中的每个表行获取每个值?

时间:2014-12-04 13:37:09

标签: javascript c# jquery asp.net-mvc visual-studio-2013

我在视图中有以下webgrid:

@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&amp;sort=SensorID&amp;sortdir=ASC">SensorID</a>
        </th>
        <th scope="col" style="display: none;">
            <a href="/PredefinedViews/CreateSensorList?selDeviceID=1&amp;sort=DeviceID&amp;sortdir=ASC">DeviceID</a>
        </th>
        <th scope="col" style="display: none;">
            <a href="/PredefinedViews/CreateSensorList?selDeviceID=1&amp;sort=SensorType&amp;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>

所以我现在想要获取每行的每个复选框(总共有六个复选框)的每个值(即使它是假的)(可以是从零到任何数字的任何数字,具体取决于SensorID的数量) (这是唯一的)有DeviceID(每行相同))。但我不知道如何遍历每一行。我喜欢使用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;
    }

我做了整个!=&#34; false&#34;因为复选框将值作为字符串返回,就像&#34; true,false&#34;形式的字符串一样。如果检查并且&#34;假&#34;如果没有检查。 我必须在这里做错的事情,我只是编辑了我的第一行(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脚本中写出类似的内容,并在每次结束迭代时将其发送给控制器?然后它开始一个新的,直到没有更多的行。

2 个答案:

答案 0 :(得分:1)

根据您的代码行,我发现您误解了某些内容

foreach (var row in sensorCollection.AllKeys) 
{

}

sensorCollection中的值不是行。它们是从视图传递的字段。

您的HTML有很多具有相同名称的输入。以一个为例:

<input id="val" name="val" type="checkbox" value="true">

sensorCollection.AllKeys将包含&#34; val&#34;的多个条目 - 网格上每行一个。您将遍历每个值,然后忽略该值并再次检索该值。本质上这个

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
    }

}

答案 1 :(得分:0)

好的,我在这里得到了答案,在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(使用我创建的方法),现在我得到了我想要的结果。