2

我正在创建一个带有数据库的剃须刀网站,但我在从网页中更改数据库元素时遇到问题。

所有元素都显示在带有更新按钮的表格中。如果单击按钮,它会将其 ID 发送到 JavaScript 函数。在该函数中,创建了一个包含所有必要属性的数组。然后我想将该数组传递给我的 razor 函数,但是在 JavaScript 中调用该函数时,出现错误(当前上下文中不存在名称“kamer”)。有人知道解决方案吗?

谢谢!

我的剃须刀功能:

@functions{
public String saveRoom(string[] room){
    var db = Database.Open("Studentenkamers"); 

    var sql = "UPDATE Studentkamer SET oppervlakte='" + room[1] + "', locatie='" + room[2] + "', type='" + room[3] + "', vrij='" + room[4] + "' WHERE id='" + room[0] + "'";
    db.Query(sql);     
    return "ok";

}

}

html和javascript代码:

@foreach(var row in db.Query(getKamers))
     {            
        <tr id="@row.id">
            <td class="td-id">@row.id</td>
            <td class="td-opp"><input type="text" value="@row.oppervlakte" name="oppervlakte" id="td-opp" /></td>
            <td class="td-loc">@row.locatie</td>
            <td class="td-type"><input type="text" value="@row.type" name="type" id="td-type" /></td>
            <td class="td-kamernr">@row.kamernr</td>
            <td class="td-vrij"><input type="text" value="@row.vrij" name="vrij" id="td-vrij" /></td>
            <td class="td-update"><input type="button" value="opslaan" onclick="updateRoom(this.id)" name="opslaan" id="@row.id" /></td>
       </tr>               
     }
    </table>
    <script type="text/javascript">

        function updateRoom(id) {               
            var opp = $("#" + id + " .td-opp input").attr("value");
            var locatie = $("#" + id + " .td-loc").html();
            var type = $("#" + id + " .td-type input").attr("value");
            var kamernr = $("#" + id + " .td-kamernr").html();
            var vrij = $("#" + id + " .td-vrij input").attr("value");

            var kamer = [id, opp, locatie, type, kamernr, vrij];
            alert(kamer);
            @saveRoom(kamer);
        }
    </script>
4

0 回答 0