2017-05-05 92 views
0

即時通訊新的MVC中,我有問題的按鈕無法調用Ajax功能渲染控制器中的操作。如何調用控制器的功能與Ajax時,單擊按鈕MVC

這種波紋管我的腳本按鈕

<input type="submit" value="show" class="btn btn-success" name="submitButton" onclick="OnActionClick()" /> 

我阿賈克斯

function OnActionClick() { 
    alert("Error when update data"); 
    $.ajax({ 
      type: "GET", 
      url: "@Url.Content("~/Report/ViewReport")", 
      data: { 
       name:'try' 
      }, 
      success: function() { 
       alert("ok"); 
      }, 
      error: function() { 
       alert("Error when update data");      
      } 
     });   
} 

,在這裏我的動作在控制器

public ActionResult ViewReport(string name) 
    { 
     Database db = new Database("Collections"); 
     List<report> data = new List<report>(); 
     data = db.Fetch<report>(@"select top 10 * from SampleTbl where name = @0",new object[]{name}); 
     return View(data); 
    } 
+0

嘗試更改URL以'網址:「報告/ ViewReport」' –

回答

0

您需要使用Url.Action()而不是使用Url.Content()

生成操作方法的標準URL。

url: '@Url.Action("ViewReport", "Report")', 

你應該type="button"

<input type="button" onclick="OnActionClick()" /> 
+0

是,等我成功使用該方法,,但我想使用Ajax,如何使它? –

+0

@IndraSuandi,你應該使用'type =「按鈕」' – Satpal

相關問題