asp.net mvc <tr>变&lt;tr/&gt; @Html.Raw 作用
2014-05-20 09:12
489 查看
遇到的问题:列表页上有查询条件,是通过将其存入到model中实现的。代码如下:<input id="inputBookShop" type="text" class="u-iptm" value="" name="inputBookShop" runat="server" readonly="readonly"/>
在JS中给它绑定:
$(function () { if ("" != "@Model.nBookShopName") { $("#inputBookShop").val("@Model.nBookShopName"); } }这个样子的话,会出现一个问题:若是在查询条件输入输入带html标签的文本,如“<tr>书店名”,点击查询后,
查询输入框中的内容就会变成“<tr/>书店名"。
解决方案是改一下JS绑定,改成:
$(function () { if ("" != "@Model.nBookShopName") { // alert("@Model.nBookShopName"); $("#inputBookShop").val("@Html.Raw(Model.nBookShopName)"); } }
这样,alert出来的虽然是带<的,但是经@Html.Raw()后在搜索框中显示的还会显示原文本。
以下:
(1)来自:http://stackoverflow.com/questions/17772553/razor-html-raw-do-not-output-text
Razor
- HTML.RAW do not output text
up vote4down votefavorite | I have tried all solution proposed to other, similar questions but none of them seems to work. In essence I am trying to display a table filled with data from collection of models. That in itself is not a problem, however I would like to force razor to generate it always in 3 columns (no matter how many elements we have). My original idea was to do it that way: <table class="projects-grid"> <tr> @for(int i = 0; i< Model.Count(); i++) { if (i != 0 && i % 3 == 0) { Html.Raw("</tr><tr>"); } var item = Model.ElementAt(i); <td class="project-tile"> @Html.DisplayFor(modelItem => item.Title) </td> } </tr> </table> So in essence every third element I would like Razor to output "" string to add another row to the table. All seems to work fine other than this sting is not present in page source. In debug I can see that this line Html.Raw("</tr><tr>"); Is actually called, but no output in generated page is present. Any help? Many thanks in advance....
|
7down voteaccepted | The reason it's not outputing is because of the context of the razor syntax being executed. In your ifblock, all code runs as if you were in a regular C# context and the line: Html.Raw("</tr><tr>"); Returns an MvcHtmlStringbut you are not doing anything with it. You need to enter an output context: @Html.Raw("</tr><tr>"); |
@Html.Raw() 方法输出带有html标签的字符串,如:
@Html.Raw("<div style='color:red'>输出字符串</div>")
结果:输出字符串
(3)来自:http://blog.csdn.net/shatamadedongxi/article/details/8756406
比如有个字符串是这样的<font color='red'>红字</font>
如果是用@Html.Raw('<font color='red'>红字</font>'),就会显示出红色的”红字“,不用的话会直接显示这段html字符串(<font color='red'>红色文字</font>)
我遇到的问题是:
我在后台定义了一个变量 string a="<div><input type="text" value="你好" /></div>";
然后在js 里写
var strHtml='@a';
$("#ruleDetaile").append(strHtml);
结果显示处理的是:<div><input type="text" value="你好" /></div>。而我想好的效果是:一个文本框里写着你好
百思不得其解 然后问了我老大 他改成了:var strHtml='@Html.Raw(a)'; 这样就可以了
相关文章推荐
- New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
- <转>ASP.NET MVC HtmlHelper用法大全
- ASP.NET MVC: how to include <span> in link from Html.ActionLink?
- ASP.NET.MVC--注解和反射将Enum转换List<SelectListItem>为@Html.DropDownListFor()下拉列表的通用方法
- 如何实现IEnumerable<T>和IEnumerator接口,一个ASP.NET MVC日志模型的实现
- ASP.NET MVC 3: Razor’s @: and <text> syntax
- asp.net mvc @Html.Raw 作用
- Note on <Pro ASP.NET MVC 4> - 01
- Nop-Fluent+AutoFAC模式在ASP.NET MVC中的应用场景<四>
- Nop-ASP.NET MVC Area概念的应用Admin后台项目<三>
- ASP.NET MVC 3: Razor’s @: and <text> syntax
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- Nop-Fluent+AutoFAC模式在ASP.NET MVC中的应用场景<四>
- asp.net mvc @Html.Raw 作用
- Nop-ASP.NET MVC Area概念的应用Admin后台项目<三>
- ASP.NET MVC DropDownList扩展,实现foreach@Html.DropDownListFor(p=>p.Type,p.Type,TypeItem)
- asp.net MVC < % = Html.XXX % > 解释
- ASP.NET MVC (Razor)开发 <<周报与绩效考核系统>> 总结与分享
- ASP.NET 4(和ASP.NET MVC 2)中输出HTML编码的新语法<%:%>
- ASP.NET中App_Code,App_Data等文件夹的作用<转>