在 VB、MVC-5 中使用 VS 2013: 这个问题涉及简单地从 View #1(在 JavaScript 代码中)调用 Controller 方法并传递字符串变量。
然后 Controller 应该永久调用 View #2。关键是我认为使用 AJAX 返回到 View #1 中的函数?
详细信息:
我在 Cesium.ScreenSpaceEventHandler 中捕获了一个左键单击事件,并且我想将确定的值(字符串)传递给 Controller 方法。然后我希望 Controller 方法调用一个新的 View 。
理论上,我的问题与Cesium无关,只是它是从点击事件处理程序中触发的。
这是第一个 View 中的 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);
这是我想从第一个 View (或类似的待定)调用的 Controller 操作方法:
<HttpPost>
Public Function JobSelected(jobSpec As String) As ActionResult
Dim result = edRepository.GetEarthDataByJobNumber(jobSpec).ToList()
Return RedirectToActionPermanent("EarthData2", result)
End Function
我已经被这个问题困扰了两天了。我已经尝试了所有能找到的方法,包括使用 Ajax(显然),但没有成功。
摘要:
我捕获了一个左键单击事件,该事件产生一个特定的作业名称。我想将该作业名称传递到一个新的 Controller 操作中,并让该操作启动一个新的 View (其模型包含该特定作业的详细信息)。
我的问题在于简单地调用 Controller 方法,传递一个变量(字符串),然后让该方法调用一个新 View 。
新信息: 我在 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 能够使用超链接中现在的查询。换句话说,超链接现在显示; ../Earth/JobSelected?=1511033 但无论我做什么,我似乎都无法提取超链接中的 1511033 。我试过:
Dim q As String = Request.Url.Query
这是我当前的 Controller 操作方法;
<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
但这不起作用。我得到一个异常(exception): 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);
Controller 代码;
<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
最佳答案
刚开始在 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);
Controller 功能;
<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
感谢所有观看或参与的人!
关于javascript - 强制 JavaScript 启动新的 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35142402/