从 TempData asp.net mvc 2 获取自定义类型
我有一个自定义类 MyCustomType。在该类中,我有一个 bool 类型的属性 MyCustomProperty 和另一个 bool 类型的属性 MyCustomProperty1。
我需要检查 MyCustomProperty 在我的视图中是否为 true。我正在做以下事情:
<%if ( TempData[ViewDataConstants.MyCustomTypeKey] != null && ((MyCustomType)TempData[ViewDataConstants.MyCustomTypeKey]).MyCustomProperty %>show some custom content.
但由于某种原因,当我运行它时,我看到错误消息,指出 MyCustomTYpe 无法找到,是否缺少程序集引用 bla-bla-bla。 MyCustomType 在我的控制器中是公开的,为了检查我什至添加了对视图的引用。但它一直说没有 MyCustomType 类。我做错了什么?
有趣的是,出于某种原因,当我将其从 Controllers 命名空间移至 Common 时,它突然起作用了。仍然不明白为什么它在控制器命名空间中不起作用。 这两个命名空间都明确包含在视图中。
I have a custom class MyCustomType. In that class I have a property MyCustomProperty of type bool and another property MyCustomProperty1 of type bool also.
I need to check if MyCustomProperty is true in my View. I`m doing the following thing:
<%if ( TempData[ViewDataConstants.MyCustomTypeKey] != null && ((MyCustomType)TempData[ViewDataConstants.MyCustomTypeKey]).MyCustomProperty %>show some custom content.
But for some reason when Im running it I see error message that MyCustomTYpe could not be found are you missing an assembly reference bla-bla-bla. MyCustomType is in my controller it
s public and to check I even added a reference to the view. But it keep saying that there`s no MyCustomType class. What am I doing wrong?
Interesting, for some reason when I moved it to Common from Controllers namespace it suddenly worked. Still dont see why its not working while in Controllers namespace.
Both namespaces were included explicitly in the view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道为什么它不起作用,但老实说,将所有这些代码放在视图中对我来说看起来是错误的。也许就像我一样,Visual Studio 不喜欢在视图中编写 C# 代码:-)。
这应该是您的视图模型上的属性:
以及您的控制器内的属性:
最后是您的视图内的属性:
现在您不再需要在视图中进行任何使用、转换等。
No idea why it didn't work but quite honestly having all this code in a view looks wrong to me. Maybe it's just like me, Visual Studio doesn't like writing C# code in a view :-).
This should be a property on your view model:
and inside your controller:
and finally inside your view:
Now you no longer need any usings, castings, ... in the view.