需要在隐藏字段中获取所选值的id,但它始终返回undefined。控制器动作和脚本如下。
我在这里做错了什么。我完全是这个UI自动完成的新手。
所以需要帮助解决这个问题。
('#PanelSearchKeyword').autocomplete({
source: function(request, response) {
var params = {};
params.supplieTyperName = document.getElementById('PanelSearchKeyword').value;
$.ajax({
type: 'POST',
dataType: "json",
url: '/Home/GetSupplierTypeNameList',
data: AddAntiForgeryToken(params),
success: function(data) {
var array = data.map(function(element) {
return {
value: element['SupplierTypeName'],
id: element['SupplierTypeId']
};
});
response(array);
},
error: function(xhr, ajaxOptions, thrownError) {
logError(ajaxOptions, thrownError);
}
});
},
select: function(event, ui) {
$("#SuppTypeId").val(ui.item.SupplierTypeId); // save selected id to hidden input
var a = $("#SuppTypeId").val();
alert(a);
//return false;
},
minLength: 2
});
我的控制器动作看起来像
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult GetSupplierTypeNameList(string supplieTyperName)
{
try
{
List<SupplierTypeViewModel> supplierTypeNameList = new List<SupplierTypeViewModel>();
supplierTypeNameList = (from supplierType in db.PPSupplierTypes
where supplierType.PPSupplierTypeName.ToUpper().StartsWith(supplierTypeName.ToUpper())
orderby supplierType.PPSupplierTypeName
select new SupplierTypeViewModel
{
SupplierTypeId = supplierType.PPSupplierTypeId,
SupplierTypeName = supplierType.PPSupplierTypeName
}).ToList();
return supplierTypeNameList;
}
catch (Exception)
{
throw;
}
return Json(suppplierTypeResult, JsonRequestBehavior.AllowGet);
}
答案 0 :(得分:3)
就我迄今为止所做的一样........ 在视图文件
中 public function search() {
if (isset($_POST['userA'])) {
$userPart = $_POST['userA'];
} else {
$userPart = "";
}
$result = $this->dbmodel->search($userPart);
$list = array();
foreach ($result as $finaldata) {
$dataN = $finaldata->name;
$dataK = $finaldata->id;
array_push($list, $dataN);
}
if ($userPart != "" && $userPart != NULL) {
echo json_encode($result);
}
}
在控制器中
function search($value) {
$this->db->select('id, name');
$this->db->where("status", "1");
$this->db->where("verification_status", "1");
$this->db->where("suspension_status", "0");
$where = "(name LIKE '%$value%'
OR address LIKE '%$value%' OR contact LIKE '%$value%')";
$this->db->where($where);
$result = $this->db->get('hotel_info');
return $result->result();
}
和dbmodel
RootAction
希望它能帮到你