ASP .NET MVC NonAction的含义 [英] ASP .NET MVC NonAction meaning

查看:94
本文介绍了ASP .NET MVC NonAction的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么我应该使用NonAction吗?我的意思是说我有一个包含多个提交值的表单:更新",删除"或插入".由于所有的提交按钮都具有相同的形式,因此我将在控制器内部切换提交值并采取相应的措施.

Can anybody please tell me why should I use NonAction? I mean say I have a form with several submit values: Update, Delete or Insert. Since all the submit buttons have the same form in common I'm switching the submit value inside the controller and act accordingly.

赞:

public ActionResult asd(string submitButton){
     switch(submitButton){
         case "Insert":
             return Insert();
         // bla bla bla
     }
}

[NonAction]
public ActionResult Insert(){
    // some code inside here
    return View();
}

再一次,为什么我应该使用NonAction代替这样的东西:

Once again, why should I use NonAction instead of something like this:

public void Insert(){
    // some code inside here
}

推荐答案

您可以省略NonAction属性,但是该方法仍然可以作为操作方法调用.

You can omit the NonAction attribute but then the method is still invokable as action method.

从MSDN站点( ref ):

默认情况下,MVC框架将控制器类的所有公共方法都视为操作方法.如果您的控制器类包含一个公共方法,并且您不希望它成为一个操作方法,则必须使用NonActionAttribute属性将该方法标记出来.

By default, the MVC framework treats all public methods of a controller class as action methods. If your controller class contains a public method and you do not want it to be an action method, you must mark that method with the NonActionAttribute attribute.

这篇关于ASP .NET MVC NonAction的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆