如何在javascript中编写SQL函数?

时间:2013-06-03 07:50:04

标签: javascript sql asp.net-mvc asp.net-mvc-4

我想通过function的用户输入textbox("@Html.TextBox("ItemID")")方法,在handle中自动获取商品的价格。

//SalesSub.cs
public class SalesSub
{
    public string ItemID { get; set; }
    public int Qty { get; set; }
    public decimal UnitPrice { get; set; }
}

//Create.cshtml
<script type="text/javascript">
function handle(e) {
     //Detect when the user press 'Enter' Or 'Tab' Key
     var keyCode = e.keyCode || e.which;
     if (keyCode === 13 || keyCode == 9) {
         var itemID = $('#ItemID').val()
         //Get the item ID in the textbox value, 
         //so how to search the price of the 
         //item in database? Using SQL or Others?
         $('#UnitPrice').val() = //Result After Search
     }
     return false;
 }

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
</script>
<label>Item ID :</label>
 **@Html.TextBox("ItemID", string.Empty, new { onkeydown = "handle(event)" })**
<label>Qty :</label>
    @Html.TextBox("Qty")
<label>Sales Price :</label>
    @Html.TextBox("UnitPrice")
}

1 个答案:

答案 0 :(得分:2)

在客户端使用Javascript,无法从SQL服务器获取数据。这里的解决方案是Web服务。通常,您可以使用两种技术“SOAP”或“REST”http://en.wikipedia.org/wiki/SOAPhttp://en.wikipedia.org/wiki/Representational_state_transfer来实现它。 据我所知,您正在使用ASP.NET MVC4。然后,您很幸运使用Web-API轻松构建REST服务:http://www.asp.net/web-api

如果你想开源:也许看http://www.openstack.org/可能有些兴趣或http://nancyfx.org/

无论如何,您必须在服务器端完成工作(检索数据),将其序列化为JSON http://en.wikipedia.org/wiki/JSON并更新您的表单。客户端用于获取数据的技术称为ajax http://en.wikipedia.org/wiki/Ajax_(programming)