Choosing user control - asp.net-mvc-3

I am using MVC3 Razor for my project. I am new to this technology.
I need to create a user control. When i am trying to add a new item it is giving me the options like
MVC3 partial page(Razor)
MVC3 View usercontrol(Aspx)
Web User Control
Which one i should select? and which folder it should be placed? in Shared folder or some other folder?

You should select MVC 3 partial page (Razor). You should just be aware that the Razor view engine does not support user controls in the way that WebForms does.
You can place it in the Shared folder if it can be used from multiple different controllers. If not, you should place it in the controller-specific views folder.

Related

Loading and posting an external Joomla form view

In Joomla 3.03 I have built a component and a form view for adding to and editing records. I want to load this form into another entirely separate view where we are writing out the records.
I know I can use jQuery.load() to pull in the form but how can I dynamically load it into place on the separate view and allow it to be completed/validated/posted as if you were on the orginal form view page?
You can use new feature of Joomla 3.0 JLayout, it enables view reuse inside or accross extensions. For more details, check in docs Sharing layouts across views or extensions with JLayout

Adding a view to MVC that doesn't belong to a controller

I'm not sure if the question title really explains what I want to do, however I will explain below:
I am using the Visual Studio MVC project template and I have changed some of the tabs to map to different actions from different controllers. However I want to make one of the tabs to open a view that will again have links for different administration actions.
The problem I have is that I am unsure where to place this view as it doesn't really belong to an admin controller as each tab on this view will link to a list view in another controller. In effect it is a sub _Layout view, as it doesn't have anything to do with a controller.
I hope I have made myself clear enough!
You can place this view in the Shared folder since it will be used by multiple controllers. Or, you could place it somewhere else and reference it by using the full path to this View/Partial View
In a Controller
public ActionResult SomeAction(){
return View("~/Path/To/View/ViewName.cshtml");
}
In a View (Razor)
#Html.RenderPartial("~/Path/To/View/ViewName.cshtml");
With that said, the Shared folder makes the most sense since it will be shared across multiple controllers.

Web page (like webmatrix) in mvc3 app

Could i include a single page .cshtml in a MVC3 app (without controller)? I have some static pages but i like to use our base layout.
Yes, you can. For example include index.cshtml with the following contents in the root of your web site:
#DateTime.Now
and then navigate to /index.cshtml.
Bare in mind that cshtml pages are not allowed in the ~/Views folder so make sure you don't put outside. The Views folder is a special one and is controlled by the ~/Views/web.config in which the base type for Razor views is changed to System.Web.Mvc.WebViewPage because those are MVC views and also they cannot be served directly.
So you could have 2 types of templates:
System.Web.WebPages.WebPage (standard WebMatrix WebPage)
System.Web.Mvc.WebViewPage (ASP.NET MVC views, stored in the ~/Views folder)
This being said, you cannot use your ~/Views/Shared/_Layout.cshtml with a WebPage. It can only be used with ASP.NET MVC views.

Using #helpers from another View in Razor ASP.Net MVC3

I want to write a few simple #helpers to use in several views.
I want them to be inside a Razor .cshtml file (not in a c# class) to have the HTML syntax highlighted.
I can easily access #helpers written within the same View, I can separate them into Helpers.cshtml, and if I put this Helpers.cshtml into an App_Code folder I can access it from any View via #Helpers.MyHelper(). But I want them to be accessible only for a few pages.
I think, it could be like putting a #using if the helpers are in c# class, but what is the namespace for just another view?..
Sorry, but that's not possible with helpers stored in the App_Code folder as .cshtml files. They will be shared among all views.

How to specify default LayoutPage in Razor in ASP.NET MVC 3 Preview 1?

I want to specify (in one place) a default layout page in Razor, so that I can delete this:
#{ LayoutPage = "~/Views/Shared/_Layout.cshtml"; }
from every .cshtml file I have. But I don't know how... Any ideas? I'm using Razor engine from ASP.NET MVC 3 Preview 1.
Create a "~/Views/_ViewStart.cshtml" page and the following inside:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Note that you can write code in here, so it is possible to change your layout based on the type of device targeted, etc.
This is now created by default in an empty MVC3 project.
Source
It looks like the way to do this is by using a _init.cshtml file in the root of the view directory in which you would like a common page element (header). When the Razor view engine builds your page it looks for a few specific files automatically called _start.cshtml, _init.cshtml, and _end.cshtml; these files are loaded in respective order by the view engine for every request. Placing the LayoutPage definition, and/or other common initialization operations in these files will ensure they're run for all pages.
Note: I'm not sure if the effect is passed down into sub-directories as it wasn't clear from the documentation; you'll have to give it a try and find out.
There's quite a bit more detailed information on how to do this found in the Microsoft how-to book on building pages with Razor. I found the section Running Code Before and After Files in a Folder on page 169. Check this Microsoft download page for the full book as well as additional Razor samples.
There is no easy way to do this in MVC 3 Preview 1. This is a limitation of the preview bits that will be addressed in upcoming releases. Unfortunately _init.cshtml files do not work in this preview of MVC3 so you cannot follow the Web Pages pattern.
There are 2 ways I can think of to make it work (though neither is optimal)
write your own page base class that derives from WebViewPage and sets the right Layout in the constructor... but in that case you would have to specify an #inherits directive in every view.
set the layout override in your action method (using the View(string viewName, string masterName) override). You could write an intermediate controller base class that would have a helper method to save yourself the trouble of repeating the layout everywhere.

Resources