在controller中 public ActionResult Sousuo()
        {
            businessDataContext db = new businessDataContext();
            
            
            if (string.IsNullOrEmpty(Request.Form["Sousuo"]))
            {
                ViewData["empty"] = "无搜索字!";
                return View();
            }
            else
            {
                string Name = Request.Form["Sousuo"];
                //List<string> SQL = new List<string>();
                //var SQL = from c in db.Sousuo
                //          where c.Name.StartsWith("%Name%")
                //          select c;
                //ViewData["SQL"] = Name;                var SQL = db.Sousuo.Where(n => n.Name.StartsWith(Name) || n.Name.EndsWith(Name) || n.Name.IndexOf(Name) != -1);
                if (SQL.Equals("0"))
                {
                    ViewData["empty2"] = "无搜索结果";
                    return View();
                }
                else
                {                    ViewData["SQL"] = SQL;
                    return View(SQL);
                }
            }
在view中
<h2>Sousuo</h2>    <table>
        <tr>
            <th>
                Name
            </th>
        </tr>
    <%=ViewData["empty"]%>
    <%=ViewData["empty2"]%>
    
  <%foreach (string c in ViewData["SQL"] as List<string>)
    { %>
    
        <tr>
            <td>
                <%=c %>
            </td>
        </tr>   
    <% } %>
报错:“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 18:     <%=ViewData["empty2"]%>
行 19:     
行 20:   <%foreach (string c in ViewData["SQL"] as List<string>)
行 21:     { %>
行 22:     
 
请教大虾,先谢了

解决方案 »

  1.   

    ViewData["empty2"]==null?"":ViewData["empty2"]
      

  2.   

     public ActionResult Sousuo()
      {
      businessDataContext db = new businessDataContext();
        
        
      if (string.IsNullOrEmpty(Request.Form["Sousuo"]))
      {
      ViewData["empty"] = "无搜索字!";
      return View();
      }
      else
      {
      ViewData["empty"]="";
      string Name = Request.Form["Sousuo"];
      //List<string> SQL = new List<string>();
      //var SQL = from c in db.Sousuo
      // where c.Name.StartsWith("%Name%")
      // select c;
      //ViewData["SQL"] = Name;  var SQL = db.Sousuo.Where(n => n.Name.StartsWith(Name) || n.Name.EndsWith(Name) || n.Name.IndexOf(Name) != -1);
      if (SQL.Equals("0"))
      {
      ViewData["empty2"] = "无搜索结果";
      return View();
      }
      else
      {
      ViewData["empty2"]="";
      ViewData["SQL"] = SQL;
      return View(SQL);
      }
      }
      

  3.   

    引用类型在调用前记得判断是否为null
      

  4.   

    谢谢各位  问题已解决
    public ActionResult Sousuo()
            {
                businessDataContext db = new businessDataContext();
                
                if (Request.Form["Sousuo"].ToString().Length==0)
                {
                    ViewData["empty"] = "无搜索字!请输入关键字后再搜索.";
           
                    return View("Empty");
                }
                else 
                {
                    string Name = Request.Form["Sousuo"];
                    var SQL = db.News.Where(n => n.Title.Contains(Name));
                    if (SQL==null)
                    {
                        ViewData["empty2"] = "无搜索结果!";
                        return View("Empty");
                    }
                    else
                    {
                        return View(SQL);                }
                    
                }  <% foreach (var item in Model) { %>
        
            <tr>
               
                <td>
                    <%= Html.ActionLink(item.Title, "content", new { id=item.NewsID }) %>
                </td>
                <td>
                    <%= Html.Encode(item.Froms) %>
                </td>
                <td>
                    <%= Html.Encode(String.Format("{0:g}", item.Time)) %>
                </td>
                <td>
                    <%= Html.Encode(item.TypeName) %>
                </td>
            </tr>
        
        <% } %>