Changing the generated code for "Add View" - asp.net-mvc

I would like for the generate "List" view contents to have a check added to it to first check that the model has elements, it occurs to me that I have no idea how these views are generated. Can those be changed?

Visual Studio uses "T4" (Text Template Transformation Toolkit) templates to generate the code when you ask for a new file, and yes, they can be edited.
Basically, you find the default templates, copy them into your project, and then edit them to create project-specific custom new-item templates which VS will use instead of the defaults.
A good set of basic instructions can be found at:
http://blogs.msdn.com/webdevtools/archive/2009/01/29/t4-templates-a-quick-start-guide-for-asp-net-mvc-developers.aspx

They are T4 templates located in:
Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 2\CodeTemplates\AddView
Also take a look at this article, it describes how to change the templates on a per project basis.

There is also another option, where in the MVC project you can make a folder called CodeTemplate and put the .tt files in there. Then those templates will be used as well.

Related

Developing Umbraco with Visual Studio - best practices

I have two environments that use Umbraco: Live and Dev.
As I have in research, Umbraco will be create a physical file when a user creates a new partial view, macro, etc. so, I have two questions:
1. What is the best way to know which files were created on Live and how to migrate them to Dev environment for the purpose of creating new features?
I have tried to Google and found "Hybrid Framework" but it is not up to date and I want to maintain code by myself.
2. I want to extend Umbraco with my own code. What is the best practice for this? (I do not want to change any code in the original Umbraco Source, just create my own new code)
As I know, I can create a package for extending code but I just want to know the other ways.
Partial Views, Macros , Templates are .cshtml files, these will not affect your source code. Same is case with css and js files; these will not harm any source code.
Just copy paste these files in respective folders and reload the nodes in backend. It will work properly.
Other way is you can create a package zip file from developer section where you can export datatypes, content and files, and install it in other environment.

Updating Linked Files in Visual Studio

I have a Universal App with t4 templates: in my Windows Project Models folder and the Windows Phone Models folder links to these files. I just update a database table with new fields and added another table, updated my edmx to reflect the new changes and I have a pcl DataModels Models folder with the original t4 templates updated also.
Is there another way to update the links in the Windows Phone folder without having to remove it and re-linking? I tried to just link the new .g.cs class, but it was listed outside the .tt file list.
Thanks!...
First; I am no expert on EF nor universal apps but if all you need is C# files to be included one way to do so is using T4Include.
First you grab T4Include of nuget and add a TT file to your project:
<#
// The relative path to project holding the source code you wish to include
ProjectFileName = #"..\SharingCode\SharingCode.csproj" ;
Namespace = "SharingCode" ;
#>
<## include file="$(SolutionDir)\packages\T4Include.1.1.4\T4\IncludeProject.ttinclude" #>
Hopefully it's a good enough fit for you.
Today I just found out how to update my linked T4 file in VS 2013 Community by right clicking the file and selecting [Run Custom Tool].

Why does this project appear to have links to files in another project?

This is the TaskyPro solution, available here: https://github.com/xamarin/mobile-samples/tree/master/TaskyPro/
If you look at the solution structure on GitHub it doesn't have anything odd about it, but when I load the solution in Visual Studio it appears as follows:
The Tasky.Android project contains the same folders as the Tasky.Core project, and it appears to have links to the files in Tasky.Core. However, when I view the folders in Windows Explorer they are empty. If I view the properties of the links, the Full Path property points to the actual file stored in the Tasky.Core project.
Why are these links here? Can I remove them?
File linking is a way to share the same files across multiple projects without having to maintain multiple copies of each file by hand. In this case, it allows you to compile the same source files for both .NET and Android. Xamarin has a lot of documentation on this, including this article which describes how Tasky is architected and implemented. I also recommend reading this guide on Sharing Code Options.

How to use common Layout and styles across multiple Asp.net MVC applications

I have a visual studio solution with multiple telerik MVC4 razor projects having same look and feel. I don't want to have same layout, CSS/styles, images and js files copied in all MVC4 projects. What is the best way to accomplish this re-usability? I tried creating a virtual directory of a commonUI project and tried to refer _layout.cshtml using http://localhost/... in _ViewStart.cshtml but it complained saying "http:/localhost/MyMvcApp/Views/Shared/_Layout.cshtml' is not a valid virtual path."
Please help!
Four recommendations:
1) Look into areas, maybe instead of having separate projects these are really different components of the same system (admin, etc.)?
2) Use add existing item in visual studio and add the items via links. This still duplicates them for deployment, but you can keep one source.
3) Consider doing this by building your own nuget package. That way although you would copy the CSS and images you would have it packaged up and could update the package as needed. The benefit is that you can update one project while not having to re-test the other (if they are separate and 1) doesn't apply).
4) I find this one uglier than the rest, but in IIS I believe you can map in folders, so you could refer to these files by links in your project, not deploy them there and then map in the appropriate folder at deployment time using a single source path.
I know of no way to share up the application path.
EDIT:
I have never tried this before, so can't vouch for it working, but maybe compile your shared items in a separate project and then reference the DLL in all other projects.
The reference I found is this, but again, I haven't confirmed that this works, just think this could be a viable path to explore:
http://www.chrisvandesteeg.nl/2010/11/22/embedding-pre-compiled-razor-views-in-your-dll/
Areas are bad because you cannot deploy them separately. Like you, I tried using virtual directories, and was successful referring to the layouts using relative syntax:
#{
Layout = "~/Common/Layouts/Layout-001.cshtml";
}
Above, the project's layout inherits the layout in the /common/ virtual directory at the root. As the common layout (along with it's bootstrap & jquery versions) evolve, you can use side-by-side version-named physical folders (like common.v01, common.v02,). So you can upgrade your applications to a newer common layout by changing the VD's path to the appropriate version.
The downside with this is you'll need to use IIS (not express) as your dev and test platform.

How to add flex project to rails project?

I'm starting to learn in flex and ruby on rails.
I see some projects there is a flex folder under rails_project/app which
contains controller,
models, views. I think it's not mannually to add these folders to rails
project.
How to do that?
Since you didn't specify an IDE this is tough to answer.
In Flash Builder, you may be able to right click on the project and "Add/Change Product Type" which would add a Flash Builder 'nature' to the project.
If all you need is access to the code; You could also add mapped directories to your rails project that point to your Flex source code located outside of the project root. This wouldn't give you 'advanced' Flash Builder features such as compiling the project when you save Flex code, but it would make the code accessible from that project.
If you don't know it yet, you might wanna look into http://flexonrails.com/
where they offer a bunch of example applications and you can see the full source codes of both flex and rails side...

Resources