如何从MVC CSHTML修剪<text>

时间:2019-06-19 15:04:09

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 razor model-view-controller

使用MVC,我的CSHTML的javascript部分具有以下if语句:

var url = '@if (@HttpContext.Current.Session["id"].ToString() == "1")
{
    <text>Testing one two three</text>
}
else
{
    @Url.Action("GetCustomer", "Customer")

}';

如果我进入 ELSE 部分,一切都很好,并生成了以下内容:

var url = '/Customer/GetCustomer';

但是,如果我进入 IF 部分,我会得到太多空白:

var url = '                  
Testing one two three  ';

我的问题是,如何修剪多余的空白并显示如下:

var url = 'Testing one two three';

先谢谢您

感谢 Ciubotariu Florin ,这是答案:

var url = '@(HttpContext.Current.Session["id"].ToString() == "1" ? "Testing one two three" : @Url.Action("GetCustomer", "Customer") )';

1 个答案:

答案 0 :(得分:1)

删除# simplistic example of what I was thinking.... # FILE file.py contains the code... def asub(p1, p2 = None, p3 = None): print(p1, p2 if p2 else 'p2 defaulted', p3 if p3 else 'p3 defaulted') return # FILE b.py contains the code... #!/usr/local/bin/python3 subs = {'file':['asub']} for key in subs: for subrt in subs[key]: print("==>", key, subrt) eval('from '+key+' import '+subrt) # on execution of b.py (i.e., ```python3 b.py``` I get the following... Traceback (most recent call last): File "b.py", line 9, in <module> eval('from '+key+' import '+subrt) File "<string>", line 1 from file import asub ^ SyntaxError: invalid syntax 标签:

<text></text>