-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")
}
4

1 回答 1

2

在客户端使用 Javascript,无法从 SQL 服务器获取数据。这里的解决方案是网络服务。通常,您使用“SOAP”或“REST” http://en.wikipedia.org/wiki/SOAPhttp://en.wikipedia.org/wiki/Representational_state_transfer两种技术之一来实现它。据我所知,您使用的是 ASP.NET MVC4。那么你有幸使用 Web-API 轻松构建 REST-Services: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)

于 2013-06-03T08:12:45.857 回答