我试图对显示在地图上的数据应用过滤器,但是由于某种原因,我设置的Ajax调用未执行。我可以在视图中到达console.log行,但此后再也不执行Ajax调用中的任何操作。这是在ASP.NET MVC中完成的。
在这个项目中,我也有其他开发人员以类似方式进行的类似Ajax调用。我试图重组我的代码,使其以类似的方式工作,但是没有成功。其他开发人员都不知道发生了什么。
控制器中的C#
[HttpPost]
public ActionResult MapFilter(string filterLake, bool filterPets)
{
var filteredProperties = db.Properties.Include(a => a.PropertyCategory).Where(b => b.Status == true).Select(x => new { x.PropertyName, x.PropertyId, x.RatePerNight, x.RatePerWeek, x.MarketingTitle, x.Latitude, x.Longitude, x.Pets, x.PropertyCategory.PropertyCategoryName });
if (filterLake != "")
filteredProperties = filteredProperties.Where(a => a.PropertyCategoryName == filterLake);
if (filterPets != true)
filteredProperties = filteredProperties.Where(a => a.Pets == filterPets);
var jsonProperties = JsonConvert.SerializeObject(filteredProperties);
return new JsonResult()
{
Data = jsonProperties,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
视图中的JavaScript和Ajax
var filterLake, filterPets;
var btnApplyFilters = document.getElementById("applyFilters");
var filterLakeNode = document.getElementById("filterLake");
var filterPetsNode = document.getElementById("filterPets");
$(document).ready(function () {
btnApplyFilters.onclick = function () {
filterLake = filterLakeNode.options[filterLakeNode.selectedIndex].value;
filterPets = filterPetsNode.options[filterPetsNode.selectedIndex].value;
console.log("Lake:|" + filterLake + "| Pets:|" + filterPets + "|");
$.ajax({
url: "/Estates/MapFilter",
type: "Post",
data: {
"filterLake": filterLake,
"filterPets": filterPets
},
success: function (result) {
filteredMapData = result;
console.log("Result = " + result);
loadMapMarkers(true)
}
});
};
})
当我在localhost上运行程序时,我可以访问
console.log("Lake:|" + filterLake + "| Pets:|" + filterPets + "|");
符合没有问题。之后的任何内容都不会运行。
参考方案
您需要检查filterPets
值,该值必须为true/false
,然后模型活页夹可以使用bool类型进行映射。
对于基本类型(bool, int, float
),应使用nullable
类型bool?
防止大小写错误的格式。
[HttpPost]
public ActionResult MapFilter(string filterLake, bool? filterPets)
{
}
如果filterPets
的值错误,则此参数为null。
我需要在网络浏览器中填写一个输入,但这不起作用。我认为必须使用name属性来完成,但是怎么做呢?foreach (HtmlElement login in webBrowser1.Document.GetElementsByTagName("input")) { if (login.GetAttribute("name"…
ddl在服务器中未更新-asp.net - javascript我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……
ajax:从json数组中提取json对象 - javascriptJSON(来自api的内部数组):[{"id":"001", "name":"john", "age":"40"}, {"id":"002", "name":"jane&…
Javascript-从当前网址中删除查询字符串 - javascript单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…
获取JavaScript值到C#字符串 - javascript是否可以在C#中执行类似的操作?该值为“ 10/05/2014”string jsValue = javascript("$('#EstimatedStartDate').val()"); 参考方案 您能否更详细地阐明您要做什么。看来您正在尝试从javascript(客户…