Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮MVC时如何使用ajax调用控制器函数_Javascript_Ajax_Asp.net Mvc - Fatal编程技术网

Javascript 单击按钮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

我是MVC新手,我有一个问题,按钮不能调用Ajax函数来渲染控制器中的操作

这是我的脚本按钮

<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()" />