在MVC中自动调用Ajax提交

时间:2014-12-04 08:14:05

标签: c# jquery ajax asp.net-mvc

我有一个奇怪的问题..这是我的jquery代码

  $("#btnRate").click(function (e) {

        alert("tık");
        e.preventDefault();

        var electionId = '@Model.ElectionId';
        var profileId = '@Model.ProfileId';

        $.ajax({
           url:  "Profile/Vote",  // '@Html.Action("Vote","Profile")',
          //  data: { electionId: electionId, profileId: profileId },
            dataType: "json",
            type: "POST",

            error: function (error) {
                alert('An error occured, please try again! ');
            },

            success: function (data) {


                if (data != null && data.success) {
                    alert("s1");
                    alert(data.url);
                    alert("s2");
                    window.location = data.url;

                } else {


                    alert('An error occured, please try again. ');

                }


            }
        });

        return false;


    });

这是html端代码

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input id="btnRate" type="submit" class="btn btn-default" value="Vote !" />
    </div>
</div>

加载详细信息表单时,会自动调用单击的btnRate按钮。但我不点击..

这是profilecontroller中的投票操作

//  [HttpPost]
    [ChildActionOnly] 
    public JsonResult Vote() //(int profileId, int electionId)
    {
        EvoteServicesProviderClient service = new EvoteServicesProviderClient();
  //    var result=  service.createPolls(electionId, profileId);
    //    if(result ==1)
      //      return Json(new { success = true, url = "/Home/ProfileStatistic?electionId=" + electionId }, JsonRequestBehavior.AllowGet);

       // else
         //   return Json(new { success = false, url = "/Home/ProfileStatistic?electionId=" + electionId }, JsonRequestBehavior.AllowGet);


       return null;
    }

即使我不点击,投票功能也是由ajax调用的。是什么原因?

编辑:这是例外

  

发生了'System.Web.HttpException'类型的异常   System.Web.dll但未在用户代码中处理

     

其他信息:执行处理程序的子请求时出错   'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper'。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题..非常有趣..  我刚刚删除了评论  //&#39; @ Html.Action(&#34;投票&#34;,&#34;个人资料&#34;)&#39;,

现在效果很好..  这是最后一部分..

 $(document).ready(function () {

    $("#btnRate").click(function(e) {

        alert("geldi");

        alert("tık");
        e.preventDefault();

        var electionId = '@Model.ElectionId';
        var profileId = '@Model.ProfileId';

        $.ajax({
            url: '@Url.Action("Vote","Profile")',  
            data: { electionId: electionId, profileId: profileId },
            dataType: "json",
            type: "POST",

            error: function (error) {
                alert('An error occured, please try again! ');
            },

            success: function (data) {


                if (data != null && data.success) {
                    alert("s1");
                    alert(data.url);
                    alert("s2");
                    window.location = data.url;

                } else {


                    alert('An error occured, please try again. ');

                }


            }
        });

        return false;
    });


});