I have MVC website, and I would like to show some code snippets as text on my website. Something like this:
List<ImageInfoModel> imgList = new List<ImageInfoModel>();
string imgPath = @"~/Content/Images/ImageGallery/";
string tmbPath = @"~/Content/Images/ImageGallery/thumbnails/";
string imgFullPath = Server.MapPath(imgPath);
string tmbFullPath = Server.MapPath(tmbPath);
If I simply copy and paste this into my view with
code text
it messes things up because of @ symbol.
So my question is: How can I display C# code inside my view?
Thanks in advance.
In your View file, add a @
wherever you start razor syntax. This will escape the Razor codes and treat it like a html code.
List<imageinfomodel> imgList = new List<imageinfomodel>
();
string imgPath = @@"~/Content/Images/ImageGallery/";
string tmbPath = @@"~/Content/Images/ImageGallery/thumbnails/";
string imgFullPath = Server.MapPath(imgPath);
string tmbFullPath = Server.MapPath(tmbPath);
如果直接来自模型,为什么不尝试@Html.Raw
或者如果它在视图中,则可以在语句前加上
@:
@Html.Raw
Use this: Code behind :
string Sk = "List<ImageInfoModel> imgList = new List<ImageInfoModel>();<br/>";
Sk += "string imgPath = @";
Sk += "\"~/Content/Images/ImageGallery/\";<br/>";
Sk += "string tmbPath = @";
Sk += "\"~/Content/Images/ImageGallery/thumbnails/\";<br/>";
Sk += "string imgFullPath = Server.MapPath(imgPath);<br/>string tmbFullPath = Server.MapPath(tmbPath);";
ViewBag.Sk = Sk;
View : @Html.Raw(ViewBag.Sk)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.