我有一个MVC 3应用程序。我想显示刚刚创建的结果。
Controller和View的一些代码。
public ActionResult ViewGenerated(string Number, string ClientID)
{
// get a list of inmate id's and pins from batch number
string url = HttpContext.Request.Url.ToString();
int client;
client = int.Parse(ClientID);
int batch = int.Parse(Number);
var list = (from a in aContext.aDetail
select a).ToList();
// set this to the object collection
ViewBag.x = list;
return View();
}
然后查看
@foreach (var r in ViewBag.x)
{
<tr>
<td>@r.ID</td>
<td>@r.Name</td>
</tr>
在我的javascript代码中,我调用一个WCF并返回该号码,我尝试将其传递给控制器?
function GenerateX() {
// blah blah
$.getJSON('/some service', function (response) {
});
// What I want is to get the number from the service then redirect the url to the view
$(this).dialog('close'); // Close it
}
我使用了一个jquery对话框来调用“GenerateX”。
我不知道该怎么做,我在这方面并不强大。
非常感谢。
更新
$.getJSON('/Services/InService.svc/blah/GeneratePINs/').success(viewPins);
// return "Number" here and want to pass it to controller.
$(this).dialog('close'); // Close it
}
function viewPins(data) {
var clientId = $("#clientIds").val();
alert(clientId); // code not reach here
window.open('/WebAdminOrion/blah/ViewGeneratedPINs?Number=' + data + '?&ClientID=' + clientId);
}
答案 0 :(得分:0)
您可以使用window.location.href
实施例
window.location.href = '/controllername/actionname';
或
window.location.href = '@Url.Action("actionname")';
答案 1 :(得分:0)
见:
HTML(对于javascript无阻碍):
<!-- Constant or key in the "web.config" having the service route -->
<input type="hidden" id="service-url" value="@ConstUrlService" />
<input type="hidden" id="action-url" value="@Url.Action("action", "controller")" />
要获得服务网址的最佳实施,请参阅jQuery AJAX calls to a WCF REST Service
JS(外部文件):
function GenerateX() {
var dialog = this;
// blah blah
$.getJSON($('#service-url').val(), function (response) {
/*
the call to "close" must be here, because the "ajax" is asynchronous
*/
/*
This answer has to be the number, and could have a response status
for example Ok (true/false)
*/
if (response.Ok && response.Number != 0) {
$(dialog).dialog('close'); // Close it
window.location = $('#action-url').val() + '?Number=' + response.Number + '&ClientID=' + response.ClientID;
}
else {
alert('problem');
}
}).fail(function() {
/*
for error
*/
alert('call error');
});
};
答案 2 :(得分:0)
我找到了答案。
window.open('/WebAdminOrion/blah/ViewGeneratedPINs?Number=' + data + '&ClientID=' + clientId);
有一个问号标记。删除它然后重新获得它。