我對MVC 3感到困惑,並且很困惑我該如何在我的項目中使用UserControls。使用@ Html.Partial呈現usercontrol(cshtml)
我已經創建了一個名爲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中呈現usercontrol的正確方法嗎?
- 更新 -
這是有效的。
@RenderPage("../Shared/MyUserControl.cshtml")
謝謝。這工作。如果我想要在加載時默認調用MyUserControl的某些操作,該怎麼辦?就像Asp.Net Usercontrol的Page_Load方法一樣。我怎麼做?我不想在Products.cshtml中調用@ {Html.RenderAction(「Index」,「MyUserControl」);}。 – Asdfg