2011-06-20 11 views
6

私はMVC 3で私の手を得ていると私のプロジェクトでUserControlsを使用する方法は混乱しています。レンダリングusercontrol(cshtml)@ Html.Partialを使用して

UserControl.cshtmlという名前のusercontrol(cshtml)ファイルを作成しましたが、Products.cshtmlをレンダリングしようとしています。

MyUserControl.cshtmlは共有フォルダにあります。 Products.cshtmlで

<div> 
    @Html.Partial("MyUserControl.cshtml"); 
</div> 

しかし、私はこのエラーを取得しています。私はそれがた.ascxファイルを検索しようとしている理由を知らない。:

The partial view 'MyUserControl.cshtml' was not found or no view engine supports the searched locations. The following locations were searched: 
~/Views/Products/MyUserControl.cshtml.aspx 
~/Views/Products/MyUserControl.cshtml.ascx 
~/Views/Shared/MyUserControl.cshtml.aspx 
~/Views/Shared/MyUserControl.cshtml.ascx 

これはMVC 3で、ユーザーコントロールをレンダリングするための正しい方法は何ですか?

- 更新日 -

これは機能します。

@RenderPage("../Shared/MyUserControl.cshtml") 

答えて

14

あなたはビューエンジンはそれを処理する、ファイルの拡張子を指定する必要はありません。

@Html.Partial("MyUserControl") 
+0

ありがとうございます。これは機能します。私がMyUserControlの何らかのアクションをロードするときにdefaultによって呼び出されるようにしたいのですが? Asp.Net UsercontrolのPage_Loadメソッドと同じです。それ、どうやったら出来るの? Products.cshtmlの@ {Html.RenderAction( "Index"、 "MyUserControl");}を呼びたくありません。 – Asdfg

関連する問題