2017-06-22 22 views
1

我想在APS.Net MVC中使用AJAX來調用我的控制器中的函數,並從模型中傳入參數,但單擊按鈕似乎並沒有調用我的控制器當我在我的代碼中放置一個斷點時。當按下「投票是」按鈕時,我希望調用VoteYes函數並將billid作爲參數傳入。 Billid是作爲@ Html.Raw(Model.BillId)檢索的頁面模型的一部分。從@ Url.Action生成的URL( 「VoteYes」, 「條例」)生成/比爾/ VoteYesASP MVC Ajax沒有打控制器功能

我Bill.cshtml代碼:

@model RepresentWebApp.Models.Bill 
@{ 
    ViewBag.Title = "Bill"; 
} 
<head> 
    <script src="~/Scripts/jquery-1.10.2.js"></script> 
    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 
</head> 

<h2>Bill</h2> 
<h3>@Html.DisplayName(Model.Title)</h3> 
<h3>@Html.DisplayName(Model.Subjects.Count().ToString())</h3> 

<button id="thebutton">Vote Yes</button> 

<script> 
$(document).ready(function() { 
    $("#thebutton").click(function (e) { 
     e.preventDefault(); 
      $.ajax({ 
       url: @Url.Action("VoteYes","Bill"), 
       type: 'POST', 
       data: { 
        BillId: @Html.Raw(Model.BillId) 
       }, 
       success: function(data){alert(data)} 
      }); 
    }) 
}) 
</script> 

我的控制器:BillController.cs

namespace RepresentWebApp.Controllers 
{ 
    public class BillController : Controller 
    { 
     private RepresentDBContext db = new RepresentDBContext(); 

     public ActionResult Index(int billid) 
     { 
      Bill bill = db.bill.Find(billid); 
      if (bill == null) 
      { 
       return HttpNotFound(); 
      } 

      return View("Bill", bill); 
     } 


     [HttpPost] 
     public ActionResult VoteYes(int BillId) 
     { 
      int itWorked = 1; 
      return View(); 
     } 

    } 
} 

以下是我加載網頁頁面時的頁面源碼:http://localhost:58556/Bill/Index/10139
10139是一個參數,它是URL中的billid。

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>Bill - My ASP.NET Application</title> 
    <link href="/Content/bootstrap.css" rel="stylesheet"/> 
<link href="/Content/site.css" rel="stylesheet"/> 

    <script src="/Scripts/modernizr-2.6.2.js"></script> 


</head> 
<body> 
    <div class="navbar navbar-inverse navbar-fixed-top"> 
     <div class="container"> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle" 
       data-toggle="collapse" data-target=".navbar-collapse"> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
       </button> 
       <a class="navbar-brand" href="/">Application name</a> 
      </div> 
     <div class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav"> 
       <li><a href="/">Home</a></li> 
       <li><a href="/Home/About">About</a></li> 
       <li><a href="/Home/Contact">Contact</a></li> 
      </ul> 
      <ul class="nav navbar-nav navbar-right"> 
       <li><a href="/Account/Register" id="registerLink">Register</a></li> 
       <li><a href="/Account/Login" id="loginLink">Log in</a></li> 
      </ul> 

     </div> 
    </div> 
</div> 
<div class="container body-content"> 




<head> 
    <script src="/Scripts/jquery-1.10.2.js"></script> 
    <script src="/Scripts/jquery.unobtrusive-ajax.js"></script> 

</head> 

<h2>Bill</h2> 
<h3>A joint resolution to authorize the use of United States Armed Forces 
against al-Qaeda, the Taliban, and the Islamic State of Iraq and Syria, and 
associated persons or forces, that are engaged in hostilities against the 
United States, the Armed Forces, or</h3> 
<h3>0</h3> 

<button id="thebutton">Vote Yes</button> 

<script> 
$(document).ready(function() { 
    $("#thebutton").click(function (e) { 
     e.preventDefault(); 
      $.ajax({ 
       url: /Bill/VoteYes, /*generates */ 
       type: 'POST', 
       data: { 
        BillId: 10139 
       }, 
       success: function(data){alert(data)} 
      }); 
    }) 
}) 
</script> 


    <hr /> 
    <footer> 
     <p>&copy; 2017 - My ASP.NET Application</p> 
    </footer> 
</div> 

<script src="/Scripts/jquery-1.10.2.js"></script> 

<script src="/Scripts/bootstrap.js"></script> 
<script src="/Scripts/respond.js"></script> 



<!-- Visual Studio Browser Link --> 
<script type="application/json" id="__browserLink_initializationData"> 
{"appName":"Chrome","requestId":"d3ed6b78215d4868a6d2d6750d9a3186"} 
</script> 
<script type="text/javascript" 
src="http://localhost:64890/6232fa8de08b494bb34022167291a681/browserLink" 
async="async"></script> 
<!-- End Browser Link --> 

</body> 
</html> 
+0

你獲得錯誤?您傳遞的數據似乎正確,但訪問操作方法時發生錯誤。 –

+0

從來沒有想過要看看那裏。獲取500錯誤無法從服務器加載資源。它的嘗試:58556 /比爾/ VoteYes不知道爲什麼這不會去我的控制器。路由問題?我不太瞭解MVC路由。 –

+0

沒關係。基洛的回答解決了它。 –

回答

4

嘗試更改瀏覽器控制檯您url: @Url.Action("VoteYes","Bill")

url: '@Url.Action("VoteYes","Bill")'

+0

解決了它!謝謝! –

+0

@JoshDennison如果它解決了您的問題,請將其標記爲將來參考的答案 – kielou