render usercontrol (cshtml) using #Html.Partial - asp.net-mvc

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")

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

Related

Html.RenderPartial - Render Partial View located in another folder

I have my view 'Create.cshtml' in folder Views-Department. I want to use partial view which is located in folder on root, 'CommonViews' with the name '_EnterpriseStructure.cshtml'.
I am using syntax RenderPartial
#{Html.RenderPartial("~/CommonViews/_EnterpriseStructure.cshtml");}
Partial view code
<div id="Client">
This is a Partial View.
</div>
When executed I am getting System.InvalidOperationException.
Detail error is:
The view at '~/CommonViews/_EnterpriseStructure.cshtml' must derive from WebViewPage, or WebViewPage.
I am using ASP.Net MVC 5 Razor Views
I have found that Razor view engine is searching in folders 'Views/Shared', 'Views/Departments' . When I moved the Partial View, to folder 'Shared' it worked.
You cannot include path, you just have to give View name. And add folders to search for View engine.
What I cannot figure out is "How to give more Search locations to View Engine?"
I would reckon you to use T4MVC nuget package in your MVC project. Once the package is included in your project, you just need to select "T4MVC.tt" file in your project (solution explorer) and say "Run Custom Tool". That's it. This will create constants for almost everything in your MVC project. That means, all views, controllers, actions, javascript files, resources, css files, images, etc are now having constants.. This way you can avoid using the hard-coded strings for views etc in your methods, and can use constants generated by T4MVC. You won't need to worry about path of views etc. T4MVC is very effective and is must for MVC projects.

Understanding MVC tags for nopCommerce

I am new to MVC , and in the application i downloaded and trying to debug i see this mark up
#Html.Widget("body_start_html_tag_after")
#Html.Partial("_Notifications")
#Html.Action("AdminHeaderLinks", "Common")
What does this mean?, #Html.Partial where can I find where the value "body_start_html_tag_after") is defined ?
And this one:
<div class="master-wrapper-main">
#RenderBody()
</div>
Where can i find what #RenderBody does?, this is in a .cshtml file.
I would suggest that you look at a reference like http://www.asp.net/mvc to gain a greater understanding of ASP.Net MVC. Having said that the #HTML.Widget, etc is server side code that gets called during the HTML generation process.
I have heard of nopCommerce but I am unfamiliar with the structure, but #Html is usually used for server side helper methods.
#Html.Partial("_Notifications") is used to add the _Notifications Partial view to the page being rendered.
#Html.Action method will render a html A tag with the href link to the controller and action to be performed.
#Html.Widget I am unfamiliar with but one could assume that it is a helper method.
#RenderBody is used on a master page (usually the shared/_Layout.cshtml) as a server side marker to render the view that comes from the associated controller.

Confusion over relatively calling partial view in asp.net mvc 3 razor engine

My app has the following structure for views...
form related views
views/form/datecontrol.cshtml
views/form/textcontrol.cshtml
views/form/checkboxcontrol.cshtml
...etc
and
search related views
views/search/searchgrid.cshtml
now,in searchgrid.cshtml, I want to make a partial call to the controls under form.
I tried all of the following but it still throws up an error.
#Html.Partial("~/form/textcontrol",
#Html.Partial("/form/textcontrol",
#Html.Partial("views/form/textcontrol",
How do i go about with this ?
Put the shared views in the "Shared" folder, and then reference them as you would any other partial:
#Html.Partial("textcontrol")
If they must be in some other folder, try (be sure to include the ~/):
#Html.Partial("~/Views/form/textcontrol.cshtml")
how i can render Partial views in asp.net mvc 3

MvcContrib FluentHtml With Razor?

I'm wondering does the latest release of MvcContrib work with the Razor view engine? I referenced the MvcContrib assembly and the FluentHtml assembly, then I added the required namespace to the ~/View/Web.config as suggested here by Darin. But still, no matter how much I try use the FluentHtml extensions in my views, it doesn't work. (nothing shows up in intellisense when I start with a dot after the html helper)
Am I missing anything?
P.S: this is the first time I use MvcContrib.
I wrote a short blog that post covers using FluentHtml with Razor.
Regarding intellisense, you will only get intellisense for FluentHtml methods on "#this." (not "#Html.") and it only list the strong-typed helpers on views that implement IViewModelContainer<T> (such as ModelWebViewPage<T>). Another possible issue is Resharper. There is a workaround for that.
I´m using mvccontrib and following darin answer it has to work. The steps I followed were:
copy-pasted in bin folder mvccontrib.dll and mvccontrib.fluent.dll
make a reference in references to those dll creating a reference for mvccontrib and mvccontrib.fluent.
two namespaces with the following names:
add namespace="MvcContrib"
add namespace="MvcContrib.FluentHtml"
in my controller I include using MvcContrib.Pagination;
finally I used in my view:
#using MvcContrib.UI.Pager
#using MvcContrib.Pagination
and rebuild.
hope it helps..

Can I create an isolated page in a project with spark masterpage setup?

Ive got a project that has got an application.spark and a html.spark that contains all the masterpage markup. The views work fine, but I need to create an isolated page that has no relation at all with the masterpages. Is it possible or will I need to have another nested masterpage?
Thanks
Did you try http://sparkviewengine.com/documentation/master-layouts using:
<use master=""/>

Resources