使用cshtml页面中SQL数据库表中的数据更新html表

时间:2014-01-03 16:09:02

标签: javascript mysql sql asp.net-mvc razor

@{ 
     var db = Database.Open("HEMS");
     var LoadDevCmd = "SELECT * FROM " + App.UserMeterID;
     var DevQuery = db.Query(LoadDevCmd);

     foreach (var row in DevQuery)
     {
        var DevType = row.DeviceType;
        var LoadType = row.Device_LoadType;
        string Loc = row.Location;
        var MAC = row.MACaddress;
        var D_Status = row.DeviceStatus;

        var myLoc = "";

        if (Loc == null)
        {

           myLoc = "XLoc";
        } 
        else
        {
            switch (Loc)
            {
                case "Living Room":
                   myLoc = "LivingRoom";

                break;

                case "Bedroom":
                   myLoc = "Bedroom";
                break;

                case "Bathroom":
                   myLoc = "Bathroom";
                break;

                case "Dining Room":
                   myLoc = "DiningRoom";
                break;

                case "Kitchen":
                   myLoc = "Kitchen";
                break;

                case "Balcony":
                   myLoc = "Balcony";
                break;

                case "Others":
                    myLoc = "Others";
                break;
           }

     }
}



<body>
    <table id="XLoc">
        <tr>
            <th align="left">Unknown Location</th>
        </tr>
    </table>

    <table id="LivingRoom">
        <tr>
            <th align="left">Living Room</th>
        </tr>
    </table>

    <table id="Bedroom">
        <tr>
            <th align="left">Bedroom</th>
        </tr>
    </table>

    <table id="Bathroom">
        <tr>
            <th align="left">Bathroom</th>
        </tr>
    </table>

    <table id="DiningRoom">
        <tr>
            <th align="left">Dining Room</th>
        </tr>
    </table>

    <table id="Kitchen">
        <tr>
            <th align="left">Kitchen</th>
        </tr>
    </table>

    <table id="Balcony">
        <tr>
            <th align="left">Balcony</th>
        </tr>
    </table>

    <table id="Others">
        <tr>
            <th align="left">Others</th>
        </tr>
    </table>

</body>

<script>

    function displayDev(id, MAC) {
        var table = document.getElementById(id);
        var row = table.insertRow(-1);
        var cell = row.insertCell(-1);
        cell.innerHTML = "MacAddress: " + MAC ;

    }

</script>

我正在尝试使用脚本函数(displayDev(id,MAC)}更新标签中的html表格。我可以知道如何在@ {}中调用脚本函数吗?

我已经尝试过该页面.ClientScriptManager.RegisterStartupScript();但我总是得到null页面导致NullReferenceException错误,我可以知道应该如何解决它?

1 个答案:

答案 0 :(得分:0)

尝试@Html.Raw("displayDev(" + myLoc + ")")