简体   繁体   English

无法调用方法,因为参数在更新mvc时被引用异常传递

[英]cannot call method because parameter is passed by reference exception on updating mvc

Recently I have updated MVC to 5.1. 最近我将MVC更新为5.1。 I am getting exception 我越来越异常了

Cannot call action method 'Void MyAd(Inspinia_MVC5_SeedProject.Models.Ad ByRef, System.String, System.String, System.String)' on controller 'Inspinia_MVC5_SeedProject.CodeTemplates.ElectronicsController' because the parameter 'Inspinia_MVC5_SeedProject.Models.Ad& ad' is passed by reference. 无法在控制器'Inspinia_MVC5_SeedProject.CodeTemplates.ElectronicsController'上调用操作方法'Void MyAd(Inspinia_MVC5_SeedProject.Models.Ad ByRef,System.String,System.String,System.String)',因为参数'Inspinia_MVC5_SeedProject.Models.Ad&ad'已通过引用。

on routes.MapMvcAttributeRoutes(); routes.MapMvcAttributeRoutes();

MyAd function is: MyAd功能是:

public void MyAd(ref Ad ad,string SaveOrUpdate,string cateogry = null,string subcategory = null)
        {
            var type = System.Web.HttpContext.Current.Request["type"];
            var isbiding = System.Web.HttpContext.Current.Request["bidingAllowed"];
            var condition = System.Web.HttpContext.Current.Request["condition"];
            var pp = System.Web.HttpContext.Current.Request["price"];
            //other huge stuff.
        }

I am using this function in different controllers. 我在不同的控制器中使用此功能。

I call the function to save data in ad object 我调用该函数将数据保存在ad对象中

MyAd(ref ad,"Save","Electronics","HomeAppliances");

and then I use data as ad.type . 然后我将数据用作ad.type

How can I tackle this exception? 我该如何处理这个例外?

(responding to an older post, but perhaps this will help someone) (回复一个较旧的帖子,但也许这会对某人有所帮助)

Passing by reference works if the scope of the method being called is not "public". 如果被调用方法的范围不是“公共”,则通过引用传递。

I was able to pass a model by reference from an Action method to a "build model" method in one controller, but not in another. 我能够通过引用将模型从Action方法传递到一个控制器中的“构建模型”方法,但不能传递给另一个控制器。 A comparison revealed that the only difference was the scope of the methods. 比较显示,唯一的区别是方法的范围。

After changing the scope from 'public' to 'private' ('protected' works too), the issue was corrected. 在将范围从“公共”更改为“私有”(“受保护”也起作用)之后,问题已得到纠正。

Seems pretty straight-forward to me. 对我来说似乎很直截了当。 It tells you right out that it cannot call this action method because it requires a pass by reference. 它告诉你它不能调用这个动作方法,因为它需要通过引用传递。 While action methods look like standard class methods, there's specific conventions and restrictions to them that are specific to actions. 虽然操作方法看起来像标准类方法,但它们具有特定于操作的特定约定和限制。 One of those is not being able to pass by reference, which is due to the way routing and model binding works in MVC. 其中一个是无法通过引用传递,这是由于路由和模型绑定在MVC中的工作方式。

Long and short, you need to change your action method signature to not pass by reference, and compensate accordingly in your action code. 无论长短,您都需要将操作方法​​签名更改为不通过引用传递,并在操作代码中进行相应补偿。

You can try a static variable in global 您可以在全局中尝试静态变量

private static test;

public ActionResult(string name)
{
   test=name;
    .....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM