【问题标题】:MVC: Get URL of Method When Using RoutingMVC:使用路由时获取方法的 URL
【发布时间】: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 浏览器的开发工具并转到网络部分并运行代码,您将看到实际请求被发送到服务器。查看那里的响应,它应该会告诉您失败的原因。
  • 查看我对问题的编辑。并感谢您的帮助!
  • 顺便说一句,大多数浏览器开发者工具都可以使用&lt;F12&gt;访问。

标签: javascript asp.net-mvc cascadingdropdown


【解决方案1】:

尝试将@Url.Action("GetStates") 更改为:

@Url.Action("ControllerName", "ActionName")

可能类似于@Url.Action("PatientSatisfactionSurvey", "GetStates") 这将生成一个类似于 ~/PatientSatisfactionSurvey/GetStates/5 的 URL,其中 5 是它从 ID 为 Country 的 html 元素中获取的 ID。

【讨论】:

  • brd4,非常感谢您的帮助。不幸的是,我得到了相同的结果。我学到了一些东西。将更新原始问题。 5. 刷新。
【解决方案2】:

终于解决了。我的问题是三个方面。首先,当我查看 HTML 源代码时,结果是这样的:

url: '@Url.Action("GetStates")', // we are calling json method

渲染如下:

url: ''

一旦我取出剃刀语法,它至少可以正确地呈现在 HTML 中。但是,仍然获取了错误的 URL。因为我使用的路线图有参数,所以我最终只为这个方法创建了一个全新的路线图:

        routes.MapRoute(
            "GetStates",
            "GetStates",
            new { controller = "Forms", action = "GetStates" },
            namespaces: new[] { "xyz.Controllers" }
        );

然后我将 javascript 中的 URL 行更改为如下所示:

url: 'GetStates'

但问题在于它只是将 /GetStates 附加到我碰巧在的任何 URL 的末尾。如果把整个完全限定的 URL 放在那里,像这样......

url: 'http://localhost:65303/GetStates'

那行得通。但由于显而易见的原因,该 URL 并不是一个长期的解决方案。所以,using this thread,我终于找到了答案。我能够获得此方法 GetStates() 的完全限定 URL,如下所示:

// Get the fully qualifed URL of the FollowUpOptions() method for use in building the cascading dropdown
UrlHelper Url = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
myModel.ullDropdownUrl = Url.Action("GetStates", "Forms", new { }, this.Request.Url.Scheme);

然后我能够在 javascript 中执行此操作,最终一切正常:

url: '@Model.fullDropdownUrl'

【讨论】:

    猜你喜欢
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多