如何防止剃刀在客户端呈现/显示值 [英] How to prevent razor from rendering/displaying value on client side

查看:80
本文介绍了如何防止剃刀在客户端呈现/显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典,其中填充了键/值对数据。



当在词典中找到键时,会显示相应的值。 />
反过来,词典项目被移除。

但是当它被删除时,返回True或False正在呈现给客户端。



以下是用于表示问题的代码的缩减版本:

 @ {
Dictionary < 字符串, 字符串 > KeyValuePairList = new Dictionary < 字符串, 字符串 > ();
KeyValuePairList.Add(Key01,Value01);
String TempValue =;
}
< div >
@if(KeyValuePairList.TryGetValue(Key01,out TempValue)){
@TempValue
@ KeyValuePairList.Remove(Key01)
}
else {
@:Value0001
}
< / div >



以下是生成的HTML:

< pre lang =HTML> < div > Value01True < / div >





我的尝试:



以下是我尝试过的一些方法。

 @ if ... 
@(KeyValuePairList.Remove(Key01))
...

OR

@ {
...
bool TempBool = false;
}
...
@if ...
@(TempBool = KeyValuePairList.Remove(Key01))
...



相同渲染:

 <   div  >  Value01True <   / div  >  





 @ if ... 
if(@ KeyValuePairList.Remove(Key01)){}
OR
@(KeyValuePairList.Remove(Key01)? :)
...



所需渲染,但使用额外的if语句:

 <   div  >  Value01 <   / div  >  





 @ if ... 
& lt;! - @ KeyValuePairList.Remove(Key01) - >
...



生成的注释为True的html,但不会显示在最终浏览器中查看:

 <   div  < span class =code-keyword>>  Value01 <! -       - >   <   / div  >  





现在的问题是,如何阻止返回值呈现给最终的HTML。

有没有一种标准的方法/最佳实践方法呢?

如果您可以指向我的源材料或解决方案,那就太棒了。

解决方案

< blockquote>

 @ if(KeyValuePairList.TryGetValue(  Key01,< span class =code-keyword> out  TempValue)){
@TempValue
@ KeyValuePairList.Remove( Key01
}
else {
@:Value0001
}



@表示渲染输出,因此您渲染KeyValuePairList.Remove的输出,如果删除了某些内容,则返回True,这样就是你的真实来自哪里。相反,你只想运行代码而不渲染输出



 @ if(KeyValuePairList.TryGetValue(  Key01 out  TempValue)){
@ TempValue
KeyValuePairList.Remove( Key01);
}
其他 {
@:Value0001
}


I have a Dictionary which is populated with Key/Value pair data.

When the Key is found in the Dictionary, the corresponding Value is displayed.
In turn the Dictionary item is removed.
But when it is removed the return of True or False is being rendered to client side.

Here is cut down version of the code used to represent the issue:

@{
	Dictionary<String, String> KeyValuePairList = new Dictionary<String, String>();
	KeyValuePairList.Add("Key01", "Value01");
	String TempValue = "";
}
<div>
	@if (KeyValuePairList.TryGetValue("Key01", out TempValue)) {
		@TempValue
		@KeyValuePairList.Remove("Key01")
	}
	else {
		@:Value0001
	}
</div>


Here is the generated HTML:

<div>Value01True</div>



What I have tried:

Here are some of the ways I have tried.

@if...
	@(KeyValuePairList.Remove("Key01"))
...

OR

@{
...
	bool TempBool = false;
}
...
@if...
	@(TempBool = KeyValuePairList.Remove("Key01"))
...


Same render:

<div>Value01True</div>



@if...
	if (@KeyValuePairList.Remove("Key01")) { }
OR
	@(KeyValuePairList.Remove("Key01")?"":"")
...


Desired render, but using additional if statement:

<div>Value01</div>



@if...
	<!--@KeyValuePairList.Remove("Key01")-->
...


Generated html with comment of True, but does not display in final browser view:

<div>Value01<!--True--></div>



Now the question is, is how to stop the return value being rendered to the final HTML.
Is there a standard way/best practice way of doing it?
If you can point me to source material or a solution, that would be great.

解决方案

@if (KeyValuePairList.TryGetValue("Key01", out TempValue)) {
    @TempValue
    @KeyValuePairList.Remove("Key01")
}
else {
    @:Value0001
}


The "@" means to render the output, so you are rendering the output of KeyValuePairList.Remove, which returns True if something has been removed, so that is where your "True" is coming from. Instead you want to just run the code and not render the output

@if (KeyValuePairList.TryGetValue("Key01", out TempValue)) {
    @TempValue
    KeyValuePairList.Remove("Key01");
}
else {
    @:Value0001
}


这篇关于如何防止剃刀在客户端呈现/显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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