Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

render usercontrol (cshtml) using @Html.Partial

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 why 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")
like image 756
Asdfg Avatar asked Jun 20 '11 18:06

Asdfg


2 Answers

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

@Html.Partial("MyUserControl")
like image 81
David Glenn Avatar answered Oct 13 '22 12:10

David Glenn


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

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

like image 44
Mangesh Pimpalkar Avatar answered Oct 13 '22 13:10

Mangesh Pimpalkar