mvc5附加信息:“对象”不包含“操作”的定义

时间:2021-07-20 13:28:18

I get:

我得到:

'object' does not contain a definition for 'Action'

excepiton in my "_ExternalLoginsListPartial" view but I don't understand why because in Login view I call:

除了我的"_ExternalLoginsListPartial"视图中但我不明白为什么因为在登录视图中我调用:

@Html.Partial("_ExternalLoginsListPartial", new { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl })

And when I look into the Model in debugger it definaltely contains "Action".

当我在调试器中查看模型时,它绝对包含了“Action”。

mvc5附加信息:“对象”不包含“操作”的定义

Can anyone help me understand that? Actually my site was running but today I started to edit "ManageUserViewModel" so that I can store some user specific settings in it. After that I always get this exception although I already reverted my changes...

有人能帮我理解吗?实际上我的站点正在运行,但是今天我开始编辑“ManageUserViewModel”,这样我就可以在其中存储一些特定于用户的设置。之后我总是得到这个例外,尽管我已经恢复了我的改变……

The code below makes my website run again:

下面的代码使我的网站再次运行:

  //string action = Model.Action;
    //string returnUrl = Model.ReturnUrl;
    string action = "ExternalLogin";
    string returnUrl = "/myTime/en/Manage";


    using (Html.BeginForm(action, "Account", new { ReturnUrl = returnUrl }))
    {
        @Html.AntiForgeryToken()
        <div id="socialLoginList">
            <p>
                @foreach (AuthenticationDescription p in loginProviders)
                {
                    <button type="submit" class="btn btn-default" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>
                }
            </p>
        </div>
    }

UPDATE: I'm able to reproducte the problem. As mentioned above I tried to change "ManageUserViewModel" so that the user can set some settings. Since I only use Google login I removed the password stuff for the model. To reproduce the exception comment out everything in ManageUserViewModel (make it an empty class). Then comment out everything in Manage:

更新:我可以繁殖问题。如前所述,我尝试更改“ManageUserViewModel”,以便用户可以设置一些设置。因为我只使用谷歌登录,所以我删除了模型的密码。要复制异常,请注释ManageUserViewModel中的所有内容(使其为空类)。然后注释掉管理中的所有内容:

 //
    // POST: /Account/Manage
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Manage(ManageUserViewModel model)
    {


        // If we got this far, something failed, redisplay form
        return View(model);
    }

and then comment in:

然后评论:

  app.UseGoogleAuthentication();

in StartupAuth.cs

在StartupAuth.cs

And you get the exception when you click the google login button. You can use the default MVC5 template and just do the steps described above to reproduce this...

当你点击谷歌登录按钮时,你会得到一个异常。您可以使用默认的MVC5模板,并按照上面描述的步骤重新生成这个模板……

I'm not sure if it is the wrong place to let my user store his settings. However, the screenshot below is definately giving me wrong information...

我不确定是否应该让我的用户存储他的设置。然而,下面的截图确实给了我错误的信息……

UPDATE2: You don't have to edit Manage function in AccountController. It is enough to make “ManageUserViewModel” empty.

UPDATE2:您不必在AccountController中编辑Manage function。这足以使“ManageUserViewModel”为空。

Cheers, Stefan

欢呼,斯蒂芬

2 个解决方案

#1


2  

The fact that it exists in the debugger is meaningless. The debugger exposes the object and all it's properties without knowing or caring about it's type. The problem you're having in your view is that you don't have a model definition, and because of that, your "model" is an object. The Object type truly does not have a property or method named Action, so you get the error.

它存在于调试器中的事实是没有意义的。调试器公开对象及其所有属性,而不知道或关心它的类型。你在视图中遇到的问题是你没有模型定义,因此,你的“模型”就是一个对象。对象类型确实没有名为Action的属性或方法,因此您会得到错误。

The best solution is to simply specify your model as the actual type you're working with. Then you get intellisense and all the other goodness that comes from being strongly-typed. The alternative, is to cast Model to dynamic, but that's really nasty.

最好的解决方案是简单地将模型指定为您正在使用的实际类型。然后你会得到智能感知和其他来自强类型的好处。另一种选择,是将模型转换为动态模型,但这真的很糟糕。

#2


0  

I encountered this exact error on this exact line because I had enabled Twitter as an authorization source with bogus key and secret values.

我在这条直线上遇到了这个错误,因为我把Twitter作为一个授权源,使用了伪造的密钥和秘密值。

mvc5附加信息:“对象”不包含“操作”的定义

When I commented that section out in StartupAuth.cs, the application worked as expected.

当我在StartupAuth中评论那部分时。cs,应用程序运行正常。

#1


2  

The fact that it exists in the debugger is meaningless. The debugger exposes the object and all it's properties without knowing or caring about it's type. The problem you're having in your view is that you don't have a model definition, and because of that, your "model" is an object. The Object type truly does not have a property or method named Action, so you get the error.

它存在于调试器中的事实是没有意义的。调试器公开对象及其所有属性,而不知道或关心它的类型。你在视图中遇到的问题是你没有模型定义,因此,你的“模型”就是一个对象。对象类型确实没有名为Action的属性或方法,因此您会得到错误。

The best solution is to simply specify your model as the actual type you're working with. Then you get intellisense and all the other goodness that comes from being strongly-typed. The alternative, is to cast Model to dynamic, but that's really nasty.

最好的解决方案是简单地将模型指定为您正在使用的实际类型。然后你会得到智能感知和其他来自强类型的好处。另一种选择,是将模型转换为动态模型,但这真的很糟糕。

#2


0  

I encountered this exact error on this exact line because I had enabled Twitter as an authorization source with bogus key and secret values.

我在这条直线上遇到了这个错误,因为我把Twitter作为一个授权源,使用了伪造的密钥和秘密值。

mvc5附加信息:“对象”不包含“操作”的定义

When I commented that section out in StartupAuth.cs, the application worked as expected.

当我在StartupAuth中评论那部分时。cs,应用程序运行正常。