在视图中从javascript调用控制器操作

时间:2021-09-21 09:32:53

I want to call action from view using JavaScript, but I can't do this: Here is my code:

我想使用JavaScript从视图中调用操作,但我不能这样做:这是我的代码:

@if (ViewBag.Status == true)
{
   <script language="javascript" type="text/javascript">
       if (confirm("Some message"));
       //And here i want to add action call
   </script>
}

I'm trying to use @Html.Action, but this ruined script code and confirm message didn't show. When I write it as shown here: Calling ASP.NET MVC Action Methods from JavaScript:

我正在尝试使用@ Html.Action,但这个破坏了脚本代码和确认消息没有显示。当我按照此处所示编写它时:从JavaScript调用ASP.NET MVC操作方法:

    @if (ViewBag.Status == true)
    {
       <script language="javascript" type="text/javascript">
           if (confirm("Some message"));
           {
                 $.ajax({
                        url: 'MyController/MyAction',
                        data: { id: id },
                        success: function () {
                            alert('Added');
                        }
                 });
          }
       </script>
    }

nothing changed. It displays confirmation dialog but don't calling method

没有改变。它显示确认对话框但不调用方法

3 个解决方案

#1


You need to change your code to something like this.In which case you would call: func(@ViewBag.Status)

您需要将代码更改为此类型。在这种情况下,您将调用:func(@ ViewBag.Status)

@Section scripts
    <script language="javascript" type="text/javascript">
        //val in this case being the value of the ViewBag passed from where the call is occurring
        function func(val) {
            if (val == true) {
                if (confirm("Some message"));
                {
                    $.ajax({
                        url: 'MyController/MyAction',
                        data: { id: id },
                        type: "POST",
                        success: function () {
                            alert('Added');
                        }
                    });
                }
            }
        }
    </script>
end section

Also in the controller remember to apply the [HttpPost] attribute on the method like so:

同样在控制器中记得在方法上应用[HttpPost]属性,如下所示:

[HttpPost]
public ActionResult MyAction(string id)
{
    // your code
    return Json();//your response
}

#2


If i understood you correctly, you want to call your Action method in controller from javascript. And show confirm message on success.

如果我理解正确,你想从javascript调用控制器中的Action方法。并在成功时显示确认消息。

Here is the code for that:

这是代码:

 if ('@ViewBag.Status' == true)
{
$.ajax({
                type: "Post",
                url: '@Url.Action("MyAction", "MyController")',
                data: { Id: Id },
                dataType: "json",
                traditional: true,
                success: function (data) {
                    alert("Success");

                },
            });
}

To hit the success, you need to return the JsonResult or ContentResult or ActionResult from controller.

要取得成功,您需要从控制器返回JsonResult或ContentResult或ActionResult。

#3


JavaScript cannot call a controller action unless you use Ajax or another client side mechanism to talk to the server!

除非您使用Ajax或其他客户端机制与服务器通信,否则JavaScript无法调用控制器操作!

What you need it to use xhr to tak to the server or use following jQuery code

您需要使用xhr来访问服务器或使用以下jQuery代码

  <script src='jquery-latest.js'></script>
  @if (ViewBag.Status == true)
  {
     <script language="javascript" type="text/javascript">
      if (confirm("Some message")){
         //And here i want to add action call
            $.get('@Url("action","controller",null)')
            .done(function(data){
                //use loaded data here
            }).fail(function(e,f,g){
               console.log({e:e,f:f,g:g})
          });
         }
     </script>
  }

PS: don't forget to reference the jQuery library

PS:别忘了引用jQuery库

#1


You need to change your code to something like this.In which case you would call: func(@ViewBag.Status)

您需要将代码更改为此类型。在这种情况下,您将调用:func(@ ViewBag.Status)

@Section scripts
    <script language="javascript" type="text/javascript">
        //val in this case being the value of the ViewBag passed from where the call is occurring
        function func(val) {
            if (val == true) {
                if (confirm("Some message"));
                {
                    $.ajax({
                        url: 'MyController/MyAction',
                        data: { id: id },
                        type: "POST",
                        success: function () {
                            alert('Added');
                        }
                    });
                }
            }
        }
    </script>
end section

Also in the controller remember to apply the [HttpPost] attribute on the method like so:

同样在控制器中记得在方法上应用[HttpPost]属性,如下所示:

[HttpPost]
public ActionResult MyAction(string id)
{
    // your code
    return Json();//your response
}

#2


If i understood you correctly, you want to call your Action method in controller from javascript. And show confirm message on success.

如果我理解正确,你想从javascript调用控制器中的Action方法。并在成功时显示确认消息。

Here is the code for that:

这是代码:

 if ('@ViewBag.Status' == true)
{
$.ajax({
                type: "Post",
                url: '@Url.Action("MyAction", "MyController")',
                data: { Id: Id },
                dataType: "json",
                traditional: true,
                success: function (data) {
                    alert("Success");

                },
            });
}

To hit the success, you need to return the JsonResult or ContentResult or ActionResult from controller.

要取得成功,您需要从控制器返回JsonResult或ContentResult或ActionResult。

#3


JavaScript cannot call a controller action unless you use Ajax or another client side mechanism to talk to the server!

除非您使用Ajax或其他客户端机制与服务器通信,否则JavaScript无法调用控制器操作!

What you need it to use xhr to tak to the server or use following jQuery code

您需要使用xhr来访问服务器或使用以下jQuery代码

  <script src='jquery-latest.js'></script>
  @if (ViewBag.Status == true)
  {
     <script language="javascript" type="text/javascript">
      if (confirm("Some message")){
         //And here i want to add action call
            $.get('@Url("action","controller",null)')
            .done(function(data){
                //use loaded data here
            }).fail(function(e,f,g){
               console.log({e:e,f:f,g:g})
          });
         }
     </script>
  }

PS: don't forget to reference the jQuery library

PS:别忘了引用jQuery库