ASP.NET MVC3如何从控制器直接引用视图 [英] ASP.NET MVC3 How to reference views directly from controller

查看:177
本文介绍了ASP.NET MVC3如何从控制器直接引用视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器我要指定一个不同的视图比默认值。
像这样的:

 公众的ActionResult EditSurvey(的Int32 ID)
    {        调查调查= _entities.Surveys.Single(S = GT; s.Id == ID);        返回视图(调查,调查);
    }

但不是指定视图作为字符串(调查)我想直接引用它,所以如果我决定改变我的观点的名称后来我不必手动更改此字符串。

所以我在寻找这样的事情:

 公众的ActionResult EditSurvey(的Int32 ID)
    {        调查调查= _entities.Surveys.Single(S = GT; s.Id == ID);        返回查看(Views.Admin.Survey,调查);
    }


解决方案

好问题,没有内置支持的查看()方法需要一个字符串,但有一个叫极好的工具 T4MVC 通过的David Ebbo 的做到了这一点。

在codePLEX的文档具有手动安装过程中,我会建议使用直接从VS2010的NuGet包管理器得到它。

其pretty简单,整个事情是你可以添加到您的项目文件。 ( T4MVC.tt T4MVC.settings.t4 ),每次你改变你的code, (1)右键单击T4MV​​C.tt和(2)点击运行自定义工具

它的作用是生成的子类,成员,属性类的所有控制器和视图。什么它甚至做的是建立强大的类型为您的所有内容,如图像,CSS,JS等(我认为这只是真棒)

例如:结果

  @ Html.RenderPartial(DinnerForm);

是:

  @ Html.RenderPartial(MVC.Dinners.Views.DinnerForm);

此:

  @ Html.ActionLink(删除晚宴,删除,晚宴,新{ID = Model.DinnerID},NULL)

应该是这个:

  @ Html.ActionLink(删除晚宴,MVC.Dinners.Delete(Model.DinnerID))

此:

 < IMG SRC =/内容/ nerd.jpg/>

将这个代替:

 < IMG SRC =@ Links.Content.nerd_jpg/>

您确实有右键点击 TT 你改变你的看法每次前文件,并提到运行自定义工具,控制器,但是,如果你想自动执行此,<一个href=\"http://blogs.msdn.com/b/davidebb/archive/2010/07/17/check-out-chirpy-a-very-cool-add-in-to-run-t4mvc-and-do-many-other-cool-things.aspx\"相对=nofollow>检查出快活这确实是多的。

注意 T4MVC对文档ASPX / MVC2的例子,但正如我在生产中使用了MVC3上MVC3工作正常/剃刀应用)

也可参阅SO T4MVC标记

In my controller I want to specify a different View than default. Like this :

public ActionResult EditSurvey(Int32 id)
    {

        Survey survey = _entities.Surveys.Single(s => s.Id == id);

        return View("Survey",survey);
    }

But instead of specifying the view as a string ("Survey") I would like to reference it directly, so if I decide to change the name of my view later on I don't have to change this string manually.

So I'm looking for something like this :

public ActionResult EditSurvey(Int32 id)
    {

        Survey survey = _entities.Surveys.Single(s => s.Id == id);

        return View(Views.Admin.Survey,survey);
    }

解决方案

Good question, there is no inbuilt support as the View() method expects a string, but there is a Nifty tool called T4MVC created by David Ebbo that does just that.

The documentation on codeplex has a manual install procedure, I would recommend getting it with NuGet package manager straight from VS2010.

Its pretty simple, the whole thing is files which you can just add to your project. (T4MVC.tt and T4MVC.settings.t4), everytime you change your code, (1) Right-click T4MVC.tt and (2) Click "Run Custom Tool".

What it does is generate a class with Subclasses, Members, Properties for all your Controllers and Views. What it even does is create strong types for all your content, like images, css, js etc. (Which I think is just awesome)

Examples:
This

@Html.RenderPartial("DinnerForm");

Would be:

@Html.RenderPartial(MVC.Dinners.Views.DinnerForm);

This:

@Html.ActionLink("Delete Dinner", "Delete", "Dinners", new { id = Model.DinnerID }, null)

Would Be this instead:

@Html.ActionLink("Delete Dinner", MVC.Dinners.Delete(Model.DinnerID))

This :

<img src="/Content/nerd.jpg" />

Would be this instead:

<img src="@Links.Content.nerd_jpg" />

You do have to Right-Click on the tt file and "Run Custom Tool" as mentioned before every time you change your views, controllers, however, if you want to automate this, Check out Chirpy which does that and more.

(Note T4MVC has aspx/mvc2 examples on the docs but works fine on MVC3 as I use in production with a MVC3/Razor app)

Also see T4MVC tag on SO.

这篇关于ASP.NET MVC3如何从控制器直接引用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆