单击有/无回发的MVC调用方法

Aln*_*dru 1 c# asp.net-mvc postback actionlink asp.net-mvc-5

我有一个关于来自视图的调用方法的问题.

基本上我的观点我有2个链接:

1链接:当我点击它时,应该调用并执行一些方法,但是网页上什么都不应该改变,所以没有回发.

2链接:当我点击它时,某些方法应该发生并且可以在同一页面上进行回发

在控制器我有:

public ActionResult FirstMethod(){ return View();}
public ActionResult SecondMethod(){ return View();}
Run Code Online (Sandbox Code Playgroud)

在视图中:

@Html.ActionLink("Action 1", "FirstMethod", "Controller");
@Html.ActionLink("Action 2", "SecondMethod", "Controller");
Run Code Online (Sandbox Code Playgroud)

所以,当我点击这两个动作发生但后来我得到一个错误说找不到FirstMethod.chtml ..

那么这可能有一种方法有回发而另一种方法没有?以及如何返回同一页面...而不是尝试获取FirstMethod.chtml ..

ram*_*ilu 7

以下解决方案基于AJAX -

控制器 -

    public class DemoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult CallMe()
        {
            return new ContentResult() { Content = "This is Demo " };
        }
}
Run Code Online (Sandbox Code Playgroud)

Index.cshtml -

<h2>Index</h2>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#Click").click(function () {
            $.ajax({
                url: "/Demo/CallMe",
                type: "GET",
            error: function (response) {
                    alert(response);
            },
            success: function (response) {
                alert(response);
            }
        });
        });
    })
</script>

<input type="button" value="Click" id="Click" />
Run Code Online (Sandbox Code Playgroud)

首先导航到/ demo/Index,它将显示带有上述标记的页面,页面上有一个按钮.当我们点击Click按钮时,我们有 -

在此输入图像描述