简体   繁体   English

C# & ASP.NET MVC:使用 ajax 调用来调用视图

[英]C# & ASP.NET MVC : call a view with ajax call

I want to call a view with an ajax call on my current view.我想在当前视图上调用 ajax 调用视图。 The following is my Ajax call that calls a function of my controller.以下是我的 Ajax 调用,它调用了我的 controller 的 function。

$.ajax({
        type: 'POST',
        url: '@Url.Action("EditCertificateObservation", "Frühwarnsystem")',
        data: {
            serverName: '@Model[0].ServerName',
            name: event.data.name,
            thumbprint: event.data.thumbprint,

            expiringDateStr: event.data.expiringDate,
            isChecked: document.getElementById(store + event.data.index).checked,
            model: data,
        },
    });

This code is my controller function that returns a view to load.此代码是我的 controller function ,它返回要加载的视图。

[HttpPost]
public ActionResult EditCertificateObservation(string serverName, string name, string thumbprint, string expiringDateStr, bool isChecked, string model)
{
    var newModel = JsonConvert.DeserializeObject<List<Store>>(model);

    var cert = new Certificate(serverName, name, thumbprint, expiringDateStr);
    var server = new Server(serverName);
    server.FetchIdByServerName();

    if (isChecked)
    {
        cert.AddToObservation(server.Id);
    }
    else
    {
        cert.DeleteFromObservation();
    }

    return View("Index");
}

To know for you: I call the ajax call with a checkbox on my view, which is dynamically generated.为您了解:我调用 ajax 调用时在我的视图上带有一个复选框,该复选框是动态生成的。 If I debug the controller function get called and runs but the browser doesn't load the view I return.如果我调试 controller function 被调用并运行,但浏览器不会加载我返回的视图。

If you need more information, just ask here.如果您需要更多信息,请在此处询问。

Thank you for your help谢谢您的帮助

You are calling an ajax POST HTTP request.您正在调用 ajax POST HTTP 请求。 It means you can download the result of the call and assign it into a javascript variable.这意味着您可以下载调用结果并将其分配给 javascript 变量。 This result will not be displayed in the browser as a page.此结果不会作为页面显示在浏览器中。 Take a look at examples of $.post here .此处查看$.post的示例。

If you want to open a view with after Ajax request than you just have to wait for the response of your controller then you can use success , but you can also use failure or error depend on your need, so your Ajax will be like this:如果您想在 Ajax 请求之后打开一个视图,那么您只需等待 controller 的响应,那么您可以使用success ,但您也可以根据需要使用failureerror ,所以您的 Z3EB7106C3477A59CDE1 会像这样:

$.ajax({
    type: 'POST',
    url: '@Url.Action("EditCertificateObservation", "Frühwarnsystem")',
    data: {
        serverName: '@Model[0].ServerName',
        name: event.data.name,
        thumbprint: event.data.thumbprint,

        expiringDateStr: event.data.expiringDate,
        isChecked: document.getElementById(store + event.data.index).checked,
        model: data,
    },
        success: function (response) { 
        alert(response.message); 
        window.location.href = "/Frühwarnsystem/Index";

        // or with some parameter
        window.location.href ="/Frühwarnsystem/Index?id=" + response.counter;

        // or if you prefer with helper ...
        window.location.href = '@Url.Action("Frühwarnsystem","Index")';
        
        },
        failure: function (response) { alert("failure"); },
        error: function (response) { alert("error"); }
});

And to be a little more useful, your controller can send a Json response with some parameter for example, as follow:为了更有用一点,您的 controller 可以发送带有一些参数的Json 响应,例如,如下所示:

[HttpPost]
public JsonResult EditCertificateObservation(string serverName, string name, string thumbprint, string expiringDateStr, bool isChecked, string model)
{
    var newModel = JsonConvert.DeserializeObject<List<Store>>(model);

    var cert = new Certificate(serverName, name, thumbprint, expiringDateStr);
    var server = new Server(serverName);
    server.FetchIdByServerName();

    if (isChecked)
    {
        cert.AddToObservation(server.Id);
    }
    else
    {
        cert.DeleteFromObservation();
    }
    // Do some condition here to send an answer ...
    string message = "";
    int counter = 0;
    var response = new { counter, message };
    return Json(response);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM