好吧......所以基本上我正在创建一个全球搜索"对于包含超过4000条记录的表格......我的大部分工作都在运行......我在ajax调用中设置了停止点,并且它提供了从下拉列表中选择的参数。
问题:在动作方法中,参数是" Null"即使我在查看JS时它的变量值仍然存在......
我正在尝试根据所选参数生成数据表......
我需要帮助将结果从AJAX调用传递给实际正在搜索表的C#方法...
这是代码......
function materialType() {
$('#search_results').html("Loading...");
var material_type = $('#material_type_name').find(':selected').text();
var search_query = $('#query').val();
var sub_type = "";
//var params = JSON.stringify({ 'search_query': search_query, 'type_name': material_type, 'sub_type_name': sub_type })
$.ajax({
url: "/AdvancedMaterialSearch/MultipleParams",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ search_query: search_query, type_name: material_type, sub_type_name: sub_type }),
dataType: 'json',
//dataType: "html",
success: function (data) {
$('#search_results').html(data);
$('#results').DataTable();
}
});
}
public ActionResult MultipleParams(string search_query,string type_name,string sub_type_name)
{
try
{
var str_query = "";
var material_type_id = 0;
var str_sub_type = "";
if (search_query != null)
{
str_query = search_query;
}
if (type_name != null)
{
material_type_id = db.t_material_types.FirstOrDefault(x => x.material_type == type_name).material_type_id;
}
if(sub_type_name != null)
{
str_sub_type = sub_type_name;
}
//check if str_query is entered but NO material_type was selected
if (!string.IsNullOrWhiteSpace(str_query) && material_type_id == 0)
{
ViewBag.SearchResults = db.t_material_master.Where(s =>s.color_code.Contains(str_query) || s.color_name.Contains(str_query) || s.description.Contains(str_query) || s.sap_number.Contains(str_query) || s.width.Contains(str_query) || s.weight.Contains(str_query) || s.size.Contains(str_query) || s.uom.material_uom_name.Contains(str_query) || s.vendor.vendor_name.Contains(str_query)).ToList();
if (ViewBag.SearchResults.Count == 0)
{
return PartialView("/Views/AdvancedMaterialSearch/SearchResultPartials/_NoResultsFound.cshtml");
}
}
//check if str_query is NOT entered but a material_type WAS selected
if (string.IsNullOrWhiteSpace(str_query) && material_type_id != 0)
{
ViewBag.SearchResults = db.t_material_master.Where(s => s.material_type_id == material_type_id).ToList();
if (ViewBag.SearchResults.Count == 0)
{
return PartialView("/Views/AdvancedMaterialSearch/SearchResultPartials/_NoResultsFound.cshtml");
}
}
//check if both str_query IS entered and a material_type WAS selected
if (!string.IsNullOrWhiteSpace(str_query) && material_type_id != 0)
{
ViewBag.SearchResults = db.t_material_master.Where(s => s.material_type_id == material_type_id && s.color_code.Contains(str_query) || s.color_name.Contains(str_query) || s.description.Contains(str_query) || s.sap_number.Contains(str_query) || s.width.Contains(str_query) || s.weight.Contains(str_query) || s.size.Contains(str_query) || s.uom.material_uom_name.Contains(str_query) || s.vendor.vendor_name.Contains(str_query)).ToList();
if (ViewBag.SearchResults.Count == 0)
{
return PartialView("/Views/AdvancedMaterialSearch/SearchResultPartials/_NoResultsFound.cshtml");
}
}
return PartialView("/Views/AdvancedMaterialSearch/SearchResultPartials/_SearchFieldOnly.cshtml");
}
catch (Exception e)
{
Debug.Print(e.Message);
return PartialView("/Views/AdvancedMaterialSearch/SearchResultPartials/_NoResultsFound.cshtml");
}
}
答案 0 :(得分:0)
不需要JSON.stringify
,只需更改ajax调用的data
,如下所示: -
data: { search_query: search_query, type_name: material_type, sub_type_name: sub_type },
并且由于您从控制器操作返回PartialView
(HTML),因此如图所示更改dataType
: -
dataType: 'html',// it should be html not json
答案 1 :(得分:0)
试试这样:
data: { search_query: search_query, type_name: material_type, sub_type_name: sub_type }
如果没有JSON.stringify,它应该可以使用,具体取决于您使用的MVC版本。