2013-06-03 114 views
-3

我想要做一個function通過handle方法的textbox("@Html.TextBox("ItemID")")用戶的輸入,自動在數據庫中獲取物品的價格。如何在JavaScript中編寫SQL函數?

//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") 
} 
+0

我想你的帖子可能已收到dowvotes,因爲它不清楚問題是什麼。你能詳細說明嗎? – fjdumont

+0

是的,首先我要允許用戶在「ItemID」文本框中鍵入項目ID,而在用戶完成後輸入項目ID並在鍵盤上按下「Enter」或「Tab」鍵,然後函數handle()可以自動獲取物品的價格。所以,我的問題是如何獲得物品的價格?它使用SQL嗎?那麼我如何在JavaScript中編寫SQL? –

回答

2

在客戶端使用Javascript,無法從SQL服務器獲取數據。這裏的解決方案是一個web服務。通常你可以使用兩種技術「SOAP」或「REST」http://en.wikipedia.org/wiki/SOAPhttp://en.wikipedia.org/wiki/Representational_state_transfer來實現它。 據我所見,你正在使用ASP.NET MVC4。那麼你是幸運的,使用網絡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)