[英]How to declare array of strings inside Razor view
I am trying to split
the string
based on ;
我试图基于split
string
;
and store it in array of string
in MVC Razor view
and I tried it as below: 并将其存储在MVC Razor view
的string
数组中,我尝试如下进行操作:
@{
string[] replies = !string.IsNullOrEmpty(messages.MessageReply) ? messages.MessageReply.Split(';') : new string []{};
}
But whenever I write this it will treat last }
of the string[] replies
as its main closing bracket instead of the one below. 但是,无论何时我写这篇文章,它都会将string[] replies
最后一个}
当作其主要的结束括号,而不是下面的括号。 How can I avoid this or how would I be declaring such sort of variables in razor view? 如何避免这种情况,或者如何在剃刀视图中声明此类变量?
Pust conditional expression into brackets: 将条件表达式放在括号中:
string[] replies = (!string.IsNullOrEmpty(messages.MessageReply) ? messages.MessageReply.Split(';') : new string []{ });
Or... just get rid of brackets: 或者...摆脱括号:
string[] replies = !string.IsNullOrEmpty(messages.MessageReply) ? messages.MessageReply.Split(';') : new string [0];
It should help 应该有帮助
I guess you could use: string[0]
instead of string[]{}
: 我猜您可以使用: string[0]
而不是string[]{}
:
@{
string[] replies = !string.IsNullOrEmpty(messages.MessageReply) ? messages.MessageReply.Split(';') : new string [0];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.