开发者

render usercontrol (cshtml) using @Html.Partial

开发者 https://www.devze.com 2023-03-14 16:43 出处:网络
I am getting my hands on MVC 3 and am confused that how do i use UserControls in my project. I have created a usercontrol (cshtml) file called UserControl.cshtml and i am trying to render it Products

I am getting my hands on MVC 3 and am confused that how do i use UserControls in my project.

I have created a usercontrol (cshtml) file called UserControl.cshtml and i am trying to render it Products.cshtml.

MyUserControl.cshtml resides in Shared folder.

In Products.cshtml:

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

But i am getting this error. I dont know wh开发者_开发百科y is it trying to search .ascx file.:

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

Is this the correct way to render usercontrol in mvc 3?

--Update--

This works.

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


You do not need to specify the file extension, the view engine will handle that.

@Html.Partial("MyUserControl")


Phil haack has fantastic blog on the way to use Partial page.

http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消