2017-05-05 87 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

相关问题