0

カスタム クラス MyCustomType があります。そのクラスには、bool 型のプロパティ MyCustomProperty と、bool 型の別のプロパティ MyCustomProperty1 があります。

ビューで MyCustomProperty が true かどうかを確認する必要があります。私は次のことをしています:

<%if ( TempData[ViewDataConstants.MyCustomTypeKey] != null && ((MyCustomType)TempData[ViewDataConstants.MyCustomTypeKey]).MyCustomProperty %>show some custom content.

しかし、何らかの理由で私m 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が公開されているときに、確認するためにビューへの参照を追加しました。しかし、MyCustomType クラスはないと言い続けています。私は何を間違っていますか?

興味深いことに、何らかの理由で Controllers 名前空間から Common に移動すると、突然機能しました。Controllers名前空間で動作しない理由はまだわかりません。両方の名前空間がビューに明示的に含まれていました。

4

1 に答える 1

1

うまくいかなかった理由はわかりませんが、正直なところ、このコードをすべてビューに表示するのは間違っているように見えます。私のように、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.
<% } %>

これで、ビューに usings、casts などは必要なくなりました。

于 2011-02-07T07:28:43.693 に答える