2017-03-17 51 views
-1

這是我的cshtml代碼和我的jQuery,我試過這兩種不同的方式。當我點擊我的按鈕批准,我希望它去帶參數的行動ApprovePoints,但沒有任何反應爲什麼我的回發不工作Html.Actionlink甚至按鈕JQuery

這裏是我的控制器:

[HttpGet] 
    public ActionResult ApprovePoints() 
    { 
     // Build out the Screen to Approve Points 
     var model = new AdminPointsViewModel(); 
     var ptsToApprove = from x in db.CommunityPts 
          join y in db.CertPointTypes on x.TypeId equals y.TypeId 
          where x.Pending == true 
          select new AdminPoints 
          { 
           communityId = x.Community.CommunityId, 
           communityName = x.Community.ComunityName, 
           pointId = x.CertPoint.PointId, 
           pointDesc = x.CertPoint.PointDescription, 
           typeId = x.CertPoint.TypeId, 
           typeDesc = y.TypeName, 
           pointsExpected = x.CertPoint.Points, 
           pointsEarned = (int) x.PointsEarned, 
           dateApplied = (DateTime) x.DateApplied, 


          }; 
     model.adminPoints = ptsToApprove; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult ApprovePoints(int cid, int pid) 
    { 
     var approvePts = db.CommunityPts.Where(x => x.CommunityId == cid && x.PointId == pid).SingleOrDefault(); 

     return View(); 

    } 
} 

這裏是標記和jQuery的:

<form method="post"> 
<table border="1" style="border-color:white; border-width:thick;"> 
    <thead style="font-weight:bold"> 
     <tr> 
      <th>Community</th> 
      <th>Activity</th> 
      <th>Category Code</th> 
      <th>Points Expected</th> 
      <th>Points Earned</th> 
      <th>Date Applied</th> 
      <th>Approve</th> 
     </tr> 
    </thead> 

    @if (Model.adminPoints != null) 
    { 
     foreach (var pts in @Model.adminPoints) 
     { 
      <tr> 
       <td>@pts.communityName</td> 
       <td>@pts.pointDesc</td> 
       <td>@pts.typeDesc</td> 
       <td>@pts.pointsExpected</td> 
       <td>@pts.pointsEarned</td> 
       <td>@Convert.ToDateTime(@pts.dateApplied).ToString("MM/dd/yyyy")</td> 
       <td>@Html.ActionLink("Approve Points", "ApprovePoints", "Admin", new {cid= @pts.communityId, pid = @pts.pointId}, new {@class = "btn btn-default"})</td> 
       <td><a href="#" class="btn btn-default" onclick="ApprovePoints(@pts.communityId, @pts.pointId);">Approve Points</a></td> 
      </tr> 
     } 

    } 
    else 
    { 
    <tr> 
     <td>There are no points to approve at this time.</td> 
    </tr> 
    } 

    <tfoot> 
     <tr> 
      <td> </td> 
      <td> </td> 
      <td> </td> 
      <td> </td> 
      <td> </td> 
      <td> </td> 
      <td> </td> 
     </tr> 
    </tfoot> 
</table> 
    </form> 
<script> 
    var ApprovePoints = (function (CommunityId, PointId) { 


     var data = new FormData(); 
     data.append("id", CommunityId); 
     data.append("pid", PointId); 

     alert(data.get("id")); 
     alert(data.get("pid")); 

     $.ajax({ 

      type: "POST", 
      url: "/Admin/ApprovePoints/", 
      data: data, 
      processData: false, 
      success: function() { 

       window.location.href = "/Admin/ApprovePoints/" 
      }, 
      error: function (errorData) { alert(errorData); } 


     }); 

    }); 
</script> 
+0

是什麼點如果你沒有做任何事情,將參數傳遞給你的發佈行動?您對數據庫執行查詢,但是您不使用查詢的結果並始終返回相同的視圖。 – CodingYoshi

回答

1

好像你可能會錯過對這裏工作的技術的一些理解。我相信你的主要誤解是你可以從JavaScript調用一個你不能的C#函數。請記住,在呈現cshtml視圖時,所有c#都通過Razor引擎呈現爲HTML。你在瀏覽器中看到的只是HTML,JavaScript和CSS。您需要調用MVC路由來調用服務器端函數。

打開瀏覽器控制檯。點擊鏈接時是否看到錯誤Uncaught ReferenceError: ApprovePoints is not defined?這是因爲onclick是一個JavaScript事件處理程序,ApprovePoints不是一個定義的函數。

您需要使表單元素提交(默認情況下,表單操作將POST到當前路由)。你可能會導致提交一個方法。例如與要素:

<form> 
    <input type="submit" value="Click to submit form"/> 
    -- or -- 
    <button>Click to submit form</button> 
</form> 

或用javscript

<script> 
function submitForm(){ 
    document.getElementById('formid').submit() 
} 
</script> 

見表格here

的文檔,你可以從這個post獲得一些見解以及

+0

嗯,我是使用MVC的新手,但在這之前我確實在HttpPost上使用過Ajax。無論如何,我重命名後的行動,並刪除了我的行動HttpPost,並行動像工作。感謝您的幫助 –

+0

優秀。如果我的文章幫助回答你的問題,不要忘記將它標記爲未來人的答案。祝你好運 – Strake