asp.net mvc <tr>变<tr/> @Html.Raw 作用
来源:互联网 发布:图论算法教材 编辑:程序博客网 时间:2024/05/16 07:16
遇到的问题:列表页上有查询条件,是通过将其存入到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>书店名”,点击查询后,
解决方案是改一下JS绑定,改成:
$(function () { if ("" != "@Model.nBookShopName") {// alert("@Model.nBookShopName"); $("#inputBookShop").val("@Html.Raw(Model.nBookShopName)"); }}
Razor - HTML.RAW do not output text
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....
Answers
The reason it's not outputing is because of the context of the razor syntax being executed. In your if
block, all code runs as if you were in a regular C# context and the line:
Html.Raw("</tr><tr>");
Returns an MvcHtmlString
but you are not doing anything with it. You need to enter an output context:
@Html.Raw("</tr><tr>");
@Html.Raw("<div style='color:red'>输出字符串</div>")
结果:输出字符串
比如有个字符串是这样的<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)'; 这样就可以了
- asp.net mvc <tr>变<tr/> @Html.Raw 作用
- asp.net mvc @Html.Raw 作用
- tr
- tr
- tr
- tr
- tr
- tr
- tr
- tr
- tr
- tr
- 【tr】
- tr
- tr
- tr
- tr
- tr
- 解决TCP报文粘连的方法
- lib和dll文件的区别和联系
- Deep learning:七(基础知识_2)
- 火车调度-c#求解-英雄会在线编程题目
- 为什么是Createthread后要CloseHandle
- asp.net mvc <tr>变<tr/> @Html.Raw 作用
- SVN常见问题集锦
- 外网登陆SAP的两种方式
- android使用ant编译APK
- SOA——面向服务的体系架构
- ORA-01591: 锁被有问题的分布式事务处理 4.22.2389 持有
- sp_addlinkedserver 与 sp_addlinkedsrvlogin 的用法
- httpclient接口调用
- 使用VC++获取文件路径,已获取当前路径为基础