C# 带/不带回发的单击MVC调用方法

C# 带/不带回发的单击MVC调用方法,c#,asp.net-mvc,postback,asp.net-mvc-5,actionlink,C#,Asp.net Mvc,Postback,Asp.net Mvc 5,Actionlink,我对视图中的调用方法有一个问题 基本上在我看来,我有两个链接: 1链接:当我点击它时,一些方法应该被调用和执行,但网页上没有任何变化,所以没有回发 2链接:当我点击它时,一些方法应该会发生,回发可以发生,在同一页上 在控制器中,我有: public ActionResult FirstMethod(){ return View();} public ActionResult SecondMethod(){ return View();} 鉴于: @Html.ActionLink("Action

我对视图中的调用方法有一个问题

基本上在我看来,我有两个链接:

1链接:当我点击它时,一些方法应该被调用和执行,但网页上没有任何变化,所以没有回发

2链接:当我点击它时,一些方法应该会发生,回发可以发生,在同一页上

在控制器中,我有:

public ActionResult FirstMethod(){ return View();}
public ActionResult SecondMethod(){ return View();}
鉴于:

@Html.ActionLink("Action 1", "FirstMethod", "Controller");
@Html.ActionLink("Action 2", "SecondMethod", "Controller");
因此,当我单击这两个操作时,都会发生,但随后会出现一个错误,即找不到FirstMethod.chtml

那么,一种方法有回发功能,另一种方法没有回发功能,这是可能的吗?以及如何返回到同一页。。。不要试图获取FirstMethod.chtml..

该方法基本上只是将您转发到指定的控制器操作,您不能更改此操作,因为这是该方法的目的

您必须处理click客户端,并将特定操作绑定到该客户端(将一些数据发布到url,之后什么也不做)。一个相当简单的方法是使用

来自上面jquery链接的示例

示例:请求test.php页面,但忽略返回结果

$.post(“test.php”)

该方法基本上只是将您转发到指定的控制器操作,您不能对此进行更改,因为这是该方法的目的

您必须处理click客户端,并将特定操作绑定到该客户端(将一些数据发布到url,之后什么也不做)。一个相当简单的方法是使用

来自上面jquery链接的示例

示例:请求test.php页面,但忽略返回结果

$.post(“test.php”)

实际上,asp.net mvc中没有回发概念。与服务器的所有交互应通过控制器/操作进行

@ActionLink()方法只生成一个链接(Html中的标记a),什么也不做。在您向控制器/操作发送请求(如单击链接)后,一切都会发生,如果您不想在单击链接时执行任何操作,最好使用如下AJAX方法

@Html.ActionLink("Action 1", "FirstMethod", "Controller", null/*routeValues*/, new { id = "link1Id" });
<script type="text/javascript">
$(function () {
    $("#link1Id").click(function () {
        $.get("/Contoller/FirstMethod", function () {
            //do nothing or alert(something)
        });
        return false;
    });
})
</script>
@Html.ActionLink(“操作1”、“第一个方法”、“控制器”,null/*routeValues*/,new{id=“link1Id”});
$(函数(){
$(“#link1Id”)。单击(函数(){
$.get(“/Contoller/FirstMethod”,函数(){
//什么也不做或提醒(某事)
});
返回false;
});
})
实际上,asp.net mvc中没有回发概念。与服务器的所有交互应通过控制器/操作进行

@ActionLink()方法只生成一个链接(Html中的标记a),什么也不做。在您向控制器/操作发送请求(如单击链接)后,一切都会发生,如果您不想在单击链接时执行任何操作,最好使用如下AJAX方法

@Html.ActionLink("Action 1", "FirstMethod", "Controller", null/*routeValues*/, new { id = "link1Id" });
<script type="text/javascript">
$(function () {
    $("#link1Id").click(function () {
        $.get("/Contoller/FirstMethod", function () {
            //do nothing or alert(something)
        });
        return false;
    });
})
</script>
@Html.ActionLink(“操作1”、“第一个方法”、“控制器”,null/*routeValues*/,new{id=“link1Id”});
$(函数(){
$(“#link1Id”)。单击(函数(){
$.get(“/Contoller/FirstMethod”,函数(){
//什么也不做或提醒(某事)
});
返回false;
});
})

在控制器操作中完成所需操作后,只需返回另一个视图即可:

public ActionResult SecondMethod()
{
    //do something    
    return View("FirstMethod");
}
看过这些之后,您很可能会对使用魔术字符串引用视图或控制器感到厌恶,这种厌恶是完全可以理解的:)


然后,您应该查看类似的内容是否适合您的需要。

您只需在控制器操作中完成所需操作后返回另一个视图即可:

public ActionResult SecondMethod()
{
    //do something    
    return View("FirstMethod");
}
看过这些之后,您很可能会对使用魔术字符串引用视图或控制器感到厌恶,这种厌恶是完全可以理解的:)


然后你应该看看这样的东西是否能满足你的需要。

下面的解决方案是基于AJAX的-

控制器-

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

        [HttpGet]
        public ActionResult CallMe()
        {
            return new ContentResult() { Content = "This is Demo " };
        }
}
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" />
索引
$(函数(){
$(“#单击”)。单击(函数(){
$.ajax({
url:“/Demo/CallMe”,
键入:“获取”,
错误:函数(响应){
警报(响应);
},
成功:功能(响应){
警报(响应);
}
});
});
})
首先导航到/demo/Index,它将使用页面中的按钮显示带有上述标记的页面。当我们点击点击按钮时,我们有-


以下解决方案基于AJAX-

控制器-

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

        [HttpGet]
        public ActionResult CallMe()
        {
            return new ContentResult() { Content = "This is Demo " };
        }
}
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" />
索引
$(函数(){
$(“#单击”)。单击(函数(){
$.ajax({
url:“/Demo/CallMe”,
键入:“获取”,
错误:函数(响应){
警报(响应);
},
成功:功能(响应){
警报(响应);
}
});
});
})
首先导航到/demo/Index,它将使用页面中的按钮显示带有上述标记的页面。当我们点击点击按钮时,我们有-


iv尝试遵循这一点,ajax总是出现错误,MVC 5使用安全吗?@AlanMorton2.0,是的,以这种方式使用绝对安全。我不知道你的代码为什么会进入错误块,请开始一个新的问题,并提供具体的细节,让我知道。我尝试过这样做,ajax总是陷入错误块,MVC 5使用安全吗?@Alanmorton 2.0,是的,以这种方式使用绝对安全。我不知道你的代码为什么会进入错误块,请用具体的细节开始一个新的问题,并让我知道。