2014-12-04 99 views
-2

调用我有一个奇怪的问题。这是我的jQuery代码阿贾克斯提交自动MVC

$("#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'。

+0

确保你是**不使用'@ Html.Action()**'在Ajax请求的url,因为当我们使用'@Html.Action出现此问题'而不是尝试使用'@ Url.Action' – 2014-12-04 08:16:17

+0

它标有'[ChildActionOnly]',所以它不能被ajax m调用ethod,所以你把它叫做别的地方。 – 2014-12-04 08:17:16

+0

@Kartikeya,我用/后/ ..”是一样的吗? – unbalanced 2014-12-04 08:20:09

回答

0

我解决了这个问题..非常有趣.. 我刚刚删除的评论 // @ Html.Action( “投票”, “档案”)“,

,现在好工作.. 这是最后一部分。这

$(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; 
    }); 


}); 
+0

刚读第一下面的评论你的问题......干杯.. !!!! – 2014-12-04 08:46:35

+0

由于'VAR electionId =“@ Model.ElectionId”;'和'变种简档=“@ Model.ProfileId”;'将永远只能是最初传递到视图中的值,在地球上,这是什么代码点? – 2014-12-04 09:02:28