首页 新闻 赞助 找找看

字符型转成字符串型

0
悬赏园豆:30 [已解决问题] 解决于 2015-01-14 15:03

在遍历中,遍历一个字符串型的viewbag.(xx数组),输出的是字符型,但是我想输出的是字符串型的,怎么转才能转成字符串型

在视图中代码如下:
 @foreach (var i in ViewBag.News)
             {
             <td>
                 @i
             </td>
             }
在controller中代码如下:
string news = "";
            string content="";
            if (newsResult.Data != null && newsResult.Data.Count > 0)
            {
                foreach (var item in newsResult.Data)
                {
                    news += item.Title;
                    content += item.Content;

                }
                news = news.Substring(0, news.Length - 1);
                content = content.Substring(0, content.Length - 1);
             
            }

            ViewBag.News = news;
            ViewBag.content = content;
乐在c#中的主页 乐在c#中 | 初学一级 | 园豆:177
提问于:2015-01-14 12:06
< >
分享
最佳答案
0

string.Join("",viewbag.(xx数组));

收获园豆:30
Yu | 专家六级 |园豆:12980 | 2015-01-14 12:34

还是没解决,在foreach中这样写还是输出的字符型

乐在c#中 | 园豆:177 (初学一级) | 2015-01-14 13:56

@乐在c#中:没码没答案

Yu | 园豆:12980 (专家六级) | 2015-01-14 14:16

@Yu: 

 在视图中如下:
@foreach (var i in ViewBag.News)
             {
             <td>
                 @i
             </td>
controller如下:
if (newsResult.Data != null && newsResult.Data.Count > 0)
            {
                foreach (var item in newsResult.Data)
                {
                    news += item.Title;
                    content += item.Content;

                }
                news = news.Substring(0, news.Length - 1);
                content = content.Substring(0, content.Length - 1);
               
            }

            ViewBag.News = news;
            ViewBag.content = content;
乐在c#中 | 园豆:177 (初学一级) | 2015-01-14 14:19

@乐在c#中: @i 不能输出?

Yu | 园豆:12980 (专家六级) | 2015-01-14 14:35

@Yu: 能啊,输出的是字符型数据,我想要@i 输出的是字符串型的数据

乐在c#中 | 园豆:177 (初学一级) | 2015-01-14 14:37

@乐在c#中: @i.ToString() 这样不行?

Yu | 园豆:12980 (专家六级) | 2015-01-14 14:44

@Yu: 额,试过了不行

乐在c#中 | 园豆:177 (初学一级) | 2015-01-14 14:45

@乐在c#中: 还有@i 输出到页面,也看不出字符型或字符串型

Yu | 园豆:12980 (专家六级) | 2015-01-14 14:46

@乐在c#中: @i 是 char类型的, 是可以ToString的

Yu | 园豆:12980 (专家六级) | 2015-01-14 14:47

@Yu: 因为我之前有试过其他的方法,出错显示为”char型不能转为string型“的错误提示,所以@i输出的是字符型,在页面显示的间隔也比较大

乐在c#中 | 园豆:177 (初学一级) | 2015-01-14 14:49

@Yu: 出错显示”i不包含Tostring定义“

乐在c#中 | 园豆:177 (初学一级) | 2015-01-14 14:52

@乐在c#中: 不会的,间隔大是CSS问题

@{     ViewBag.Title = "Index"; } 
<style>     
td {         margin: 0;         padding: 0;     }
</style>
<h2>Index</h2>
<table cellspacing="0" >     
<tr>         
@foreach (var i in ViewBag.News)         
{             
<td>                 
@Convert.ToString(i)            
 </td>         
}    
 </tr>
</table>
Yu | 园豆:12980 (专家六级) | 2015-01-14 14:54

@乐在c#中: 是 ToString注意大小写

Yu | 园豆:12980 (专家六级) | 2015-01-14 14:56

@Yu: 谢谢,解决了,你的方法可行

乐在c#中 | 园豆:177 (初学一级) | 2015-01-14 15:02
其他回答(2)
0

多贴一点,行不?

幻天芒 | 园豆:37175 (高人七级) | 2015-01-14 13:19

在视图中,用foreach遍历一个string型的viewbag.xx数组输出的是字符型,我想把字符型转成字符串型

支持(0) 反对(0) 乐在c#中 | 园豆:177 (初学一级) | 2015-01-14 13:58

@乐在c#中: 

@foreach (char i in ViewBag.News) { <td> @(i.ToString()) </td> }

支持(0) 反对(0) 幻天芒 | 园豆:37175 (高人七级) | 2015-01-14 15:03
0

你的代码有问题吧 

news += item.Title;

content += item.Content;

这拼接起来的 会是数组么? 还有

news = news.Substring(0, news.Length - 1); content = content.Substring(0, content.Length - 1);

看这个情况你应该是想要逗号或者其他字符分开吧  会不会是news += item.Title+","; 才对  然后

ViewBag.News = news.split(',');

mushishi | 园豆:230 (菜鸟二级) | 2015-01-15 12:39

还有ViewBag是动态类型 使用的时候需要强转一下foreach(var new in  (string[])ViewBag.News )

支持(1) 反对(0) mushishi | 园豆:230 (菜鸟二级) | 2015-01-15 12:41

@mushishi: 难道不是数组也能遍历吗?还有我就是想强转才出现char不能转为string型的错误

支持(0) 反对(0) 乐在c#中 | 园豆:177 (初学一级) | 2015-01-16 10:45

@乐在c#中:  比如string str="abc";   str[0] 是'a'是一个char 当然不能转成string类型

支持(0) 反对(0) mushishi | 园豆:230 (菜鸟二级) | 2015-01-16 15:26

@mushishi: 原来是这样啊,明白了,谢谢

支持(0) 反对(0) 乐在c#中 | 园豆:177 (初学一级) | 2015-01-16 15:28

@乐在c#中:  嗯哈。字符串是'串'。是字符的组合 而不是它爹。自然转不了。嘿嘿

支持(0) 反对(0) mushishi | 园豆:230 (菜鸟二级) | 2015-01-18 13:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册