0

我正在 ASP MVC Razor 文件中尝试以下代码:

       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);             
        }

但我收到一条错误消息:

object' 不包含 'Contains' 的定义和最佳扩展方法重载

4

2 回答 2

7
var topic = ViewData["TopicID"];

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

于 2012-08-15T06:15:54.963 回答
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);
    }
于 2012-08-15T06:17:26.670 回答