2017-11-11 109 views
2

我想用自動填充選定項填寫我的表單數據。我的自動填充工作正常,但我無法弄清楚如何通過從自動填充文本框中檢索項來填寫表單數據。這裏是我的代碼使用AJAX和Jquery自動完成功能填寫表單數據

[HttpPost] 
public JsonResult GetAutocomplete(string term) 
{ 
    var custo = (from customer in db.tbl_Customers 
       where customer.Name.Contains(term) 
       select new { label = customer.Name, val = customer.customer_ID }).ToList(); 
    return Json(custo); 
} 

[HttpPost] 
public JsonResult GetDetails(string id) 
{ 
    tbl_Customers custodetail = db.tbl_Customers.Single(x => x.customer_ID.ToString() == id); 
    return Json(custodetail, JsonRequestBehavior.AllowGet); 
} 

在CSHTML查看

function custoautocomplete() 
{ 
    $(function() { 
     $("#Customer_ID").autocomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        cache: false, 
        async: false, 
        url: '/Orders/GetAutocomplete/', 
        data: "{ 'term': '" + request.term + "'}", 
        dataType: "json", 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        success: function (data) { 
         response($.map(data, function (item) { 
          return item; 
         })), 
         function (item) { 
          $.ajax({ 
           cache: false, 
           async: false, 
           type: "POST", 
           url: '/Orders/GetDetails/', 
           data: { "id": data.item.Customer_ID}, 

          success: function (data) { 
           $('#Customer_Contact').val(data.Customer_Contact) 
           $("#Billing_Address").val(data.Billing_Address) 
           $("#Shipping_Address").val(data.Shipping_Address) 
          } 
         }); 
        } 
       }); 
      } 
     }); 
    }); 
} 
+0

u能請讓我知道,怎麼樣。我在阿賈克斯新。 – Zaini

+0

N.P.我在等待 – Zaini

回答

0

您需要處理.select事件,讓你的AJAX調用有更新基於所選值的DOM Ajax的功能。你也應該在source事件的Ajax調用的變化如下

$("#Customer_ID").autocomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      cache: false, 
      // async: false, NEVER do this 
      url: '@Url.Action("GetAutocomplete", "Orders")', // don't hard code your url's 
      data: { term: request.term }, // change 
      dataType: "json", 
      type: "POST", 
      // contentType: "application/json; charset=utf-8", delete 
      success: function (data) { 
       response($.map(data, function (item) { 
        return { 
         label: item.label, 
         id: item.id 
        } 
       })); 
      } 
     }) 
    },select: function(event, ui) { 
     $.ajax({ 
      cache: false, 
      type: "POST", 
      url: '@Url.Action("GetDetails", "Orders")', 
      data: { id: ui.item.value }, 
      success: function (data) { 
       $('#Customer_Contact').val(data.Customer_Contact) 
       $("#Billing_Address").val(data.Billing_Address) 
       $("#Shipping_Address").val(data.Shipping_Address) 
      } 
     }); 
    } 
}); 

指出作爲一個側面說明,你不需要JsonRequestBehavior.AllowGetGetDetails()方法,因爲它標誌着[HtpPost]。此外,您應該返回一個匿名對象(有沒有點對面時,你永遠不使用它的網絡發送額外的數據),例如

var custodetail = db.tbl_Customers 
    .Single(x => x.customer_ID == id) // .ToString() not required - see below 
    .Select(x => new 
    { 
     Customer_Contact = x.Customer_Contact, 
     .... 
    }; 

,你的參數應該是一樣的customer_ID類型,例如

public JsonResult GetDetails(int id) 

也東東修改GetAutocomplete

var custo = (from customer in db.tbl_Customers 
      where customer.Name.Contains(term) 
      select new { label = customer.Name, id = customer.customer_ID }); 
return Json(custo); 
+0

現在我的行​​正在動態添加,我想根據行索引填充行數據。我將如何實現這一目標? @Stephen Muecke – Zaini

+0

@Zaini,你需要提出一個新的問題和相關的細節。 –

+0

好吧,我會在幾個薄荷糖發佈我的問題。 – Zaini

相關問題