【发布时间】:2015-06-03 18:24:47
【问题描述】:
我找到了this tutorial,了解如何使用 Razor 语法在 MVC 中构建级联下拉菜单。我按照教程进行操作,让它在自己的项目中完美运行。但是现在我正试图将它移植到我的实际项目中,当第一个下拉列表被更改时,我得到了一个错误。根据脚本,会弹出一条警报:
Failed to retrieve states: [object Object]
我不知道 [object Object] 是什么意思。我的猜测是错误与 URL 有关:
url: '@Url.Action("GetStates")
但这只是猜测。示例项目和真实项目的主要区别在于,真实项目对 URL 使用路由以下是整个脚本:
<script src="~/Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"</script>
<script type="text/javascript">
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#Country").change(function () {
$("#State").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetStates")', // we are calling json method
dataType: 'json',
data: { id: $("#Country").val() },
// here we are get value of selected country and passing same value as input to json method GetStates.
success: function (states) {
// states contains the JSON formatted list
// of states passed from the controller
$.each(states, function (i, state) {
$("#State").append('<option value="' + state.Value + '">' +
state.Text + '</option>');
// here we are adding option for States
});
},
error: function (ex) {
alert('Failed to retrieve states: ' + ex);
}
});
return false;
})
});
之后编辑:
在 Chrome 的开发者工具中观察网络流量时,我在独立项目中进行了这项工作,并看到了标题为“GetStates”的条目和这个 URL:http://localhost:50266/CustomerFeedback/GetStates。
我在我的实际项目中又做了一次,这次我看到一个条目,上面写着“45/”,这个 URL:http://localhost:65303/PatientSatisfactionSurvey/45/。
我认为这证实了我对 URL 是问题的怀疑。我将不得不弄清楚如何使这个 URL 有效。
另一个编辑:
关于可行的项目,如果我去:http://localhost:50266/CustomerFeedback/GetStates
我明白了:
Server Error in '/' Application.
This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
这是意料之中的,因为我正在尝试使用该实际方法。意思是,我可以转到 方法的 URL。但是当我尝试在我的项目中做同样的事情时:http://localhost:65303/PatientSatisfactionSurvey/GetStates,它只是加载页面。那是因为它认为“GetStates”是一个参数,而不是一个方法。
我不知道该方法的 URL 是什么!!该死的路由正在阻碍....
routes.MapRoute(
"PatientSatisfactionSurvey",
"PatientSatisfactionSurvey/{ApptID}/{*LanguageCode}",
new { controller = "Forms", action = "PatientSatisfactionSurvey" },
namespaces: new[] { "GEDC.Controllers" }
);
【问题讨论】:
-
您是否使用调试器或 console.dir() 或其他工具查看过对象 ex 内部的内容?
-
我很想知道该怎么做!它是 javascript,所以它不会达到标准断点。我对 MVC 和 jquery 太了解了,不知道如何调试它,或者 console.dir() 是什么意思。我会开始寻找,但如果您有任何快速指示,我将不胜感激。
-
如果您打开 Web 浏览器的开发工具并转到网络部分并运行代码,您将看到实际请求被发送到服务器。查看那里的响应,它应该会告诉您失败的原因。
-
查看我对问题的编辑。并感谢您的帮助!
-
顺便说一句,大多数浏览器开发者工具都可以使用
<F12>访问。
标签: javascript asp.net-mvc cascadingdropdown