开发者

How can I insert text from an external file into my MVC3 Razor view?

My razor web is big and padded with a lot of text that I would like to move into an external file. Right now it looks like this:

@if (Model.RowKey == "ABC") {
    <div class="mdl">
       50+ lines of textual information. Not code.
    </div>
}
@if (Model.RowKey == "DEF") {
    <div class="mdl">
       50+ lines of different textual information. Not code.
    </div>
}

Is there a way that I can store this text externally so I don't use too much space in my view? Also IF I store externally is there an overhead when the view is created? Will it开发者_运维知识库 have to recompile the view each time? This is very important as I want things to run quick.


If Model in your code is the view's Model and not an enumerator variable (and if your content is purely static) you could try with a different view for each type of RowKey, and then in your controller do:

Return View("ABC");

Then in your Views folder you would have ABC.cshtml, DEF.cshtml, XYZ.cshtml, etc. All text will still be in the view (not really a problem there), but you will get rid of all those @if() blocks.


You could use a Text.resx file to store the text and use Resources.Text.ABC to retrieve the text in the View

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜