2012-08-15 100 views
0

我想一个ASP MVC剃刀文件中下面的代码:错误消息xx.Contains在C#

 var topic = ViewData["TopicID"]; 
     var mustBeReplaced = string.Empty; 
     var topicValue = Model.Topic; 
     var replaceResult = string.Empty; 
     if (topic.Contains(topicValue)) { 
      mustBeReplaced = "value=\"" + topicValue + "\""; 
      replaceResult = mustBeReplaced + " selected=\"selected\""; 
      topic = topic.Replace(mustBeReplaced, replaceResult);    
     } 

但我得到一个错误信息:

对象不包含'Contains'的定义以及过载的最佳扩展方法

回答

7
var topic = ViewData["TopicID"]; 

返回对象。你需要转换为字符串。

2

试试这个

var topic = (string)ViewData["TopicID"]; 
    var mustBeReplaced = string.Empty; 
    var topicValue = "11111"; 
    var replaceResult = string.Empty; 
    if (topic.Contains(topicValue)) 
    { 
     mustBeReplaced = "value=\"" + topicValue + "\""; 
     replaceResult = mustBeReplaced + " selected=\"selected\""; 
     topic = topic.Replace(mustBeReplaced, replaceResult); 
    }