【问题标题】:Cannot implicitly convert type 'void' to 'object' MVC cshtml method call无法将类型“void”隐式转换为“object”MVC cshtml 方法调用
【发布时间】:2013-12-12 20:45:11
【问题描述】:

我在从 Mvc-Project 中的 cshtml 文件调用简单方法时遇到了一些问题。这是给我错误的代码:

@{
var chatHub = new TestClass();
}
@chatHub.TestMethod();

它是“@chatHub.TestMethod();”这给了我“无法将类型'void'隐式转换为'object'”错误。

这是测试方法:

public void TestMethod()
        {
            ChatHub test = new ChatHub();
            //var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
            test.JoinGrupprum1();
        }

这是Joingrupprum1:

public void JoinGrupprum1()
        {
            Groups.Add(Context.ConnectionId, "Grupprum1");
        }

知道有什么问题吗?

【问题讨论】:

    标签: asp.net-mvc razor signalr


    【解决方案1】:

    @chatHub.TestMethod(); 之前使用@,您指示Razor 将chatHub.TestMethod() 的结果打印到HTML。为了打印一些东西,它会在结果上调用object.ToString()

    但是,您的方法是 void,因此不会返回任何内容。如果您不打算打印任何内容,请将其移到大括号内:

    @{
        var chatHub = new TestClass();
        chatHub.TestMethod();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-08
      • 2019-02-13
      • 2020-07-31
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 2016-02-17
      相关资源
      最近更新 更多