将razorview的按钮与控制器动作绑定

时间:2019-03-15 13:02:54

标签: c# asp.net-mvc model-view-controller razor-pages

我需要绑定这样的按钮:

 <button type="button" onclick="location.href='@Url.Action("Compress", "Home")'" />

具有这样的控制器动作:

 public void Compress()
    {
        _compressingService.CompressAllFiles();
    }

服务CompressAllFiles();没有获得值,只是在服务器中压缩文件。 这就是为什么我的控制器方法无效的原因,但是按下此按钮我有:System.NullReferenceException: 'Object reference not set to an instance of an object.异常。怎么了?可能是我错误地绑定了它,或者我的控制器动作签名无效?谢谢

1 个答案:

答案 0 :(得分:0)

引发异常System.NullReferenceException: 'Object reference not set to an instance of an object.,因为您尝试访问的对象为null。在这种情况下,_compressingService将需要实例化或注入