从 TempData asp.net mvc 2 获取自定义类型

发布于 2024-10-16 07:34:25 字数 616 浏览 6 评论 0原文

我有一个自定义类 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 its 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2024-10-23 07:34:25

不知道为什么它不起作用,但老实说,将所有这些代码放在视图中对我来说看起来是错误的。也许就像我一样,Visual Studio 不喜欢在视图中编写 C# 代码:-)。

这应该是您的视图模型上的属性:

public class MyViewModel
{
    public bool MyCustomProperty { get; set; }
}

以及您的控制器内的属性:

public ActionResult Foo()
{
    var model = TempData[ViewDataConstants.MyCustomTypeKey] as MyCustomType ?? new MyCustomType();
    var viewModel = Mapper.Map<MyCustomType, MyViewModel>(model);
    return View(viewModel);
}

最后是您的视图内的属性:

<% if (Model.MyCustomProperty) { %>
    show some custom content.
<% } %>

现在您不再需要在视图中进行任何使用、转换等。

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:

public class MyViewModel
{
    public bool MyCustomProperty { get; set; }
}

and inside your controller:

public ActionResult Foo()
{
    var model = TempData[ViewDataConstants.MyCustomTypeKey] as MyCustomType ?? new MyCustomType();
    var viewModel = Mapper.Map<MyCustomType, MyViewModel>(model);
    return View(viewModel);
}

and finally inside your view:

<% if (Model.MyCustomProperty) { %>
    show some custom content.
<% } %>

Now you no longer need any usings, castings, ... in the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文