最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Calling controller action from javascript in view - Stack Overflow

programmeradmin1浏览0评论

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

@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:

    @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

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

@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:

    @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

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked May 12, 2015 at 12:39 Kamil RafałkoKamil Rafałko 3302 gold badges3 silver badges15 bronze badges 7
  • 1 possible duplicate of Calling ASP.NET MVC Action Methods from JavaScript – jrummell Commented May 12, 2015 at 12:43
  • what does your javascript console say ? – Aivan Monceller Commented May 12, 2015 at 13:34
  • Where I can find it? I'm using visual studio 2013 – Kamil Rafałko Commented May 12, 2015 at 13:37
  • When i use location.href = "@Url.Action("MyAction")" it works fine : o – Kamil Rafałko Commented May 12, 2015 at 13:47
  • If you remove the if condition, does the ajax call to your controller happen at all? Try debugging your javascript, using for example chrome dev tools (F12) and see what happens. – elolos Commented May 12, 2015 at 13:53
 |  Show 2 more ments

3 Answers 3

Reset to default 3

You need to change your code to something like this.In which case you would call: 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]
public ActionResult MyAction(string id)
{
    // your code
    return Json();//your response
}

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

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.

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

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

  <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

发布评论

评论列表(0)

  1. 暂无评论