Javascript 单击按钮MVC时如何使用ajax调用控制器函数
我是MVC新手,我有一个问题,按钮不能调用Ajax函数来渲染控制器中的操作 这是我的脚本按钮Javascript 单击按钮MVC时如何使用ajax调用控制器函数,javascript,ajax,asp.net-mvc,Javascript,Ajax,Asp.net Mvc,我是MVC新手,我有一个问题,按钮不能调用Ajax函数来渲染控制器中的操作 这是我的脚本按钮 <input type="submit" value="show" class="btn btn-success" name="submitButton" onclick="OnActionClick()" /> 这是我在控制器中的动作 public ActionResult ViewReport(string name) { Database db = new D
<input type="submit" value="show" class="btn btn-success" name="submitButton" onclick="OnActionClick()" />
这是我在控制器中的动作
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);
}
public ActionResult视图报告(字符串名称)
{
数据库db=新数据库(“集合”);
列表数据=新列表();
data=db.Fetch(@“从SampleTbl中选择前10个*,其中name=@0”,新对象[]{name});
返回视图(数据);
}
您需要使用而不是使用Url.Content()
生成操作方法的完全限定URL
您应该type=“button”
您应该使用@Url.Action()而不是@Url.Content()。
请参阅以下内容了解两者之间的区别:
是的,我成功地使用了该方法,但我想使用ajax,如何实现它?@IndraSuandi,您应该使用
type=“button”
尝试将url更改为url:“Report/ViewReport”
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);
}
url: '@Url.Action("ViewReport", "Report")',
<input type="button" onclick="OnActionClick()" />