mvc5附加信息:'object'不包含'Action'的定义

时间:2014-03-03 17:49:25

标签: asp.net-mvc asp.net-mvc-5

我明白了:

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

除了我的“_ExternalLoginsListPartial”视图,但我不明白为什么因为在Login视图中我打电话:

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

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

enter image description here

任何人都可以帮我理解吗? 实际上我的网站正在运行,但今天我开始编辑“ManageUserViewModel”,以便我可以在其中存储一些用户特定的设置。之后我总是得到这个例外,虽然我已经恢复了我的更改......

以下代码让我的网​​站再次运行:

  //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>
    }

更新: 我能够重现这个问题。如上所述,我尝试更改“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);
    }

然后发表评论:

  app.UseGoogleAuthentication();
在StartupAuth.cs中

当您点击Google登录按钮时,您会收到异常。 您可以使用默认的MVC5模板,只需执行上述步骤即可重现此...

我不确定让我的用户存储他的设置是不是错误的地方。但是,下面的屏幕截图肯定会给我错误的信息......

UPDATE2: 您不必在AccountController中编辑Manage函数。只需将“ManageUserViewModel”清空即可。

干杯, 斯蒂芬

2 个答案:

答案 0 :(得分:2)

它存在于调试器中这一事实毫无意义。调试器在不知道或关心它的类型的情况下公开对象及其所有属性。您在视图中遇到的问题是您没有模型定义,因此,您的模型&#34;是objectObject类型确实没有名为Action的属性或方法,因此您会收到错误。

最佳解决方案是简单地将您的模型指定为您正在使用的实际类型。然后你得到intellisense和强力打字所带来的所有其他善良。另一种方法是将Model投射到dynamic,但那真的很讨厌。

答案 1 :(得分:0)

我在这条确切的行上遇到了这个确切的错误,因为我已将Twitter作为授权来源启用了伪造密钥和机密值

enter image description here

当我在StartupAuth.cs 中注释掉该部分时,应用程序按预期工作。