强制JavaScript启动新的控制器操作

时间:2016-02-01 23:28:53

标签: javascript jquery ajax asp.net-mvc vb.net

在VB中使用VS 2013,MVC-5: 这个问题涉及简单地调用一个Controller方法,并从View#1传递一个字符串变量(在JavaScript代码中)。

然后,Controller应永久调用View#2。关键是我认为使用AJAX返回View#1中的函数?

详细信息:

我在Cesium.ScreenSpaceEventHandler中捕获了一个左键单击事件,我想将确定的值(字符串)传递给Controller方法。然后,我希望Controller方法调用一个新视图。

理论上,我的问题与Cesium无关,只不过它是在click事件处理程序中触发的。

以下是第一个视图中的JavaScript代码:

var leftClickHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);

leftClickHandler.setInputAction(function (action) {
    //reset visibility of selectionIndicator and infoBox
    viewer.selectionIndicator.viewModel.selectionIndicatorElement.style.visibility = 'visible';
    document.getElementsByClassName('cesium-infoBox')[0].style.visibility = "visible";

    var pickedObject = viewer.scene.pick(action.position);
    //don't do anything if we didn't click on an object
    if (!Cesium.defined(pickedObject)) { return; }

    //don't do anything if it's not the entity we care about
    //if (entity !== pickedObject.id) { return; }

    //if this is the entity we care about, hide the selectionIndicator and infoBox
    viewer.selectionIndicator.viewModel.selectionIndicatorElement.style.visibility = 'hidden';
    document.getElementsByClassName('cesium-infoBox')[0].style.visibility = "hidden";

    var myEntity = pickedObject.id;
    //alert('Picked: ' + myEntity.name);

    $.ajax({
        url: '@Url.Action("JobSelected", "Earth")',
        type: "POST",
        dataType: 'text',
        contentType: 'application/json; charset=utf-8',
        cache: false,
        data: myEntity.name,
        success: function () {
            alert("okay!");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("Error: " + errorThrown + jqXHR);
        }
    });
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

这是我想要从第一个视图(或类似的 - TBD)调用的控制器操作方法:

<HttpPost>
Public Function JobSelected(jobSpec As String) As ActionResult
    Dim result = edRepository.GetEarthDataByJobNumber(jobSpec).ToList()

    Return RedirectToActionPermanent("EarthData2", result)
End Function

我被困在这两天了。我已经尝试了所有我能找到的东西,包括使用Ajax(显然),但无济于事。

摘要:

我捕获了一个产生特定作业名称的左键单击事件。我想将该作业名称传递给新的控制器操作,并让该操作启动一个新的视图(其模型包含该特定作业的详细信息)。

我的问题在于简单地调用控制器方法,传递单个变量(字符串),然后让方法调用新视图。

新信息: 我已使用View中的以下代码替换了Ajax调用:

            var myEntity = pickedObject.id;
            //alert("'" + myEntity.name + "'");

            var myUrl = '@Url.Action("JobSelected", "Earth")';
            var newUrl = myUrl + '?=' + myEntity.name.replace(/ /g, "");
            var newUrl2 = newUrl.replace("HTI#", "");
            var newUrl3 = newUrl2.substr(0, newUrl2.indexOf("-"));

            window.location.replace(newUrl3);

其中大部分都是先前返回的myEntity.name字符串的清理。

好的,所以这个帖子很棒(感谢您的帮助),但现在我似乎无法使我的Controller能够使用现在在超链接中的查询。换句话说,超链接现在显示; 的 ../地球/ JobSelected?= 1511033 但无论我做什么,我似乎都无法提取超链接中的1511033。我试过了:

            Dim q As String = Request.Url.Query

这是我当前的Controller Action方法;

        <ActionName("JobSelected")>
    Function JobSelected() As ActionResult
        Dim q As String = Request.Url.Query
        Dim result = edRepository.GetEarthDataByHtiJobNumber(q).ToList()

        'The following works fine, so my issue is with obtaining the value (q) to be passed...
        'Dim result = edRepository.GetEarthDataByHtiJobNumber("1503069").ToList()

        Return View("EarthData2", result)
    End Function

但那不起作用。我得到一个例外: System.ArgumentOutOfRangeException 有什么建议吗?

好吧,我找到了我需要的答案。以下作品; JavaScript代码:

            var myEntity = pickedObject.id;
            //alert("'" + myEntity.name + "'");

            var myUrl = '@Url.Action("JobSelected", "Earth")';
            var newUrl = myUrl + '?jobNumber=' + myEntity.name.replace(/ /g, "");
            var newUrl2 = newUrl.replace("HTI#", "");
            var newUrl3 = newUrl2.substr(0, newUrl2.indexOf("-"));

            window.location.replace(newUrl3);

控制器代码;

        <ActionName("JobSelected")>
    Function JobSelected() As ActionResult
        Dim q As String = Request.Url.Query

        q = Replace(q, "?jobNumber=", "")
        Dim result = edRepository.GetEarthDataByHtiJobNumber(q).ToList()

        Return View("EarthData2", result)
    End Function

1 个答案:

答案 0 :(得分:0)

刚刚在StackOverflow(长期用户)上发帖,很抱歉,我把答案放在了编辑中。

这是答案: 的 JavaScript的:

        var leftClickHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);

        leftClickHandler.setInputAction(function (action) {
            //reset visibility of selectionIndicator and infoBox
            viewer.selectionIndicator.viewModel.selectionIndicatorElement.style.visibility = 'visible';
            document.getElementsByClassName('cesium-infoBox')[0].style.visibility = "visible";

            var pickedObject = viewer.scene.pick(action.position);
            //don't do anything if we didn't click on an object
            if (!Cesium.defined(pickedObject)) { return; }

            //don't do anything if it's not the entity we care about
            //if (entity !== pickedObject.id) { return; }

            //if this is the entity we care about, hide the selectionIndicator and infoBox
            viewer.selectionIndicator.viewModel.selectionIndicatorElement.style.visibility = 'hidden';
            document.getElementsByClassName('cesium-infoBox')[0].style.visibility = "hidden";

            var myEntity = pickedObject.id;
            //alert("'" + myEntity.name + "'");

            var myUrl = '@Url.Action("JobSelected", "Earth")';
            //The following is just clean-up code dealing with myEntity.name
            //Take out the spaces, and add the Query question and equals sign
            var newUrl = myUrl + '?jobNumber=' + myEntity.name.replace(/ /g, "");
            //Take out the leading, unwanted header
            var newUrl2 = newUrl.replace("HTI#", "");
            //Take out the trailing text, after the job #
            var newUrl3 = newUrl2.substr(0, newUrl2.indexOf("-"));
            //Redirect to the new page url and fire it up
            window.location.replace(newUrl3);

        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

控制器功能;

        <ActionName("JobSelected")>
    Function JobSelected() As ActionResult
        Dim q As String = Request.Url.Query

        q = Replace(q, "?jobNumber=", "")
        Dim result = edRepository.GetEarthDataByHtiJobNumber(q).ToList()

        Return View("EarthData2", result)
    End Function

感谢观看或参与过的所有人!