从javascript调用razor函数

时间:2012-09-13 09:59:15

标签: html razor javascript

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

所有元素都显示在带有更新按钮的表格中。如果单击该按钮,则会将其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>

0 个答案:

没有答案