How to get a 'codebehind' file for an ASP.NET-MVC View in RC1 to be created by default

邮差的信 提交于 2019-12-01 06:03:16

To answer your question directly, I don't think you can change this default. You could try modifying the template (which would be somewhere in %programfiles%\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates"), but I don't know for sure.

However, the "MVC-way" for this scenario is probably to create a custom helper, in a separate class.

I recently wrote a web app that used Gravatar (http://www.gravatar.com) to generate profile pictures and I kept writing the same custom <img> tags all over my views, so I created a helper: Html.Gravatar()

Just create a static class "MyHelpers" or "GravatarHelpers" or "FooHelpers" and add static extension methods with signatures like this:

public static string Gravatar(this HtmlHelper htmlHelper, string emailAddress) {
    return htmlHelper.Image(...);
}

or, if you use strongly typed views (ViewPage<T>) and want to take advantage of that you can extend HtmlHelper<T>

public static string Foo<TModel>(this HtmlHelper<TModel> htmlHelper, ...) {
    // Do stuff
    return // Stuff
}

You can easily switch out HtmlHelper for UrlHelper or AjaxHelper. I believe you can also access the ViewData, etc. from a ViewContext property on the helper.

Marc Gravell

That really doesn't seem like a great idea. Every commentary I've read has said "don't use the code-behind; we wish it wasn't there". And I agree. You could try writing your logic in extension methods (traditionally on HtmlHelper) like here.

I am of the opinion that code behind files are still a useful thing. While many people talk about how they are bad because they provide a method to break the design pattern that MVC strives for, they do not inherently do this. You can say that the view is supposed to be stupid, but I think its probably more accurate to say that the view isn't suppose to concern itself with complex business logic. It does however need to concern it self with the aspects of presentation. As such, there are still elements to presenting information that require code beyond simple html. It follows the reasoning of separation of concerns (let alone, pure readability of the view) that the markup concern itself with markup with the code behind supporting it where code is necessary.

Luis Abreu has written a good article about the subject

Codebehind files in asp.net mvc are not evil

FYI - In Visual Studio 2010, simply adding the appropriately named code file to the file containing the view will automatically associate the new code-behind file with the view. You don't need to remove and re-add the files to get them to link up. However, you'll still need to change the inherited classes to make it work as @Harry describes.

I would also like to have code-behinds as default, can't find the way to change it though. This page explains how to add a codebehind to a page: http://msdn.microsoft.com/en-us/magazine/cc301556.aspx.

In my opinion code-behind classes are ideal for databinding controls and doing some minor changes to the data in case it's necessary (for example date and currency formatting). Also Repeaters are by far the easiest way to display data lists, I still cant find a way to iterate through an anonymous type collection, and only having a for statement in a page reminds me of PHP :S

Andrew Harry

I answered this question here:

How to add a Code-behind page to a Partial View

Seems this wasn't particularly tricky, and is quite do-able This answer worked for a Partial 'ViewUserControl' but the same should apply

Ok.

First: Add a Class file with the convention of .cs (i.e. view.ascx.cs)

Second: Add "using System.Web.Mvc;" to the class

Third: Change the Class to Inherit from "ViewUserControl<>"

Fourth: Add the following to the View's header:

CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"

Fifthly: Copy the files out of the solution and drag back in to reassociate the two together

Note: For this to work with a Normal MVC View you just need to inherit the class from "ViewPage"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!