搜索框中的无效文字未点击Else语句

时间:2012-09-26 16:01:43

标签: c# asp.net-mvc-3 html5

Aspx引擎

当我在文本框中输入无效的用户ID并点击搜索时,会显示“未找到类别”。现在显示“搜索结果”。似乎没有达到其他声明。

HTML

<div align="center">
    <form id="searchUser" method="post" action="Search">
        <table align="center">
    <tr>
        <td class="label">
            Enter ID:
        </td>
        <td>
            <input type="text" name="UserId" id="UserId" value="<%=(string)(ViewBag.userid)%>" />
        </td>
    </tr>
      <tr style ="display:none;">
        <td class="label">
            Email:
        </td>
        <td>
            <input type="text" name="Email" id="Text1" value="<%=(string)(ViewBag.email)%>" />
        </td>
    </tr>
      <tr style ="display:none;">
        <td class="label">
            lastFourDigits:
        </td>
        <td>
            <input type="text" name="LastFourDigits" id="Text2" value="<%=(string)(ViewBag.lastFourDigits)%>" />
        </td>
    </tr>
    <tr>
        <td>
            <button class="searchButton" id="searchButton">Search</button>
        </td>
    </tr>
  </table>
 </form>
  </div>
  <hr /> 

  <% if (ViewBag.searchClass !=null)
    { %>
    <h2>Search Resuls</h2>
    <br />
    <%List<AAlexUsers.Models.SearchClass> searchClassList= ViewBag.searchClass;%>
    <%if(searchClassList.Count>0)
    <table>
          <%foreach (AAlexUsers.Models.SearchClass searchClass in searchClassList)
     {%>
      <tr>
         <td>
            UserID:
        </td>
        <td class="content">
          <%=searchClass.userId%>
        </td>
    </tr>
    <tr>
        <td>
            Email:
        </td>
        <td class="content">
         <%=searchClass.email%>
        </td>
    </tr>
    <tr>
        <td>
        Last Four Digits:
        </td>
        <td class="content">
          <%=searchClass.lastFourdigits%>
         </td>
    </tr>
    <%} %>
    <%} %>
 </table>

    <%} else %>
 <%{ %>
    <h2>No Class found.</h2>
 <%} %>

控制器

public ActionResult Search()
{
    string userId = Request["UserId"];
    string email = Request["Email"] ?? string.Empty;
    string lastFourdigits = Request["LastFourDigits"] ?? string.Empty;

    bool view = false;

    if (string.IsNullOrEmpty(userId))
    {
        view = true;
    }
    if (!view)
    {
        List<AAlexUsers.Models.SearchClass> searchClass = 
            AAlexUsers.Models.SearchClass.Users(userId,email,lastFourdigits);
        {
            ViewBag.searchClass = searchClass;
            ViewBag.lastFourdigits = lastFourdigits;
            ViewBag.userId = userId;
            ViewBag.email = email;
        }
    }
    return View();
}

模型

public class SearchClass
{
    public string userId { get; set; }
    public string email { get; set; }
    public string lastFourdigits { get; set; }

    public static List<AAlexUsers.Models.SearchClass> 
        Users(string userId, string email, string lastFourdigits)   
    {
        SearchClass Alex = new SearchClass();
        Alex.userId = "1234";
        Alex.email = "Alex@gmail.com";
        Alex.lastFourdigits = "1885";

        SearchClass Emilio = new SearchClass();
        Emilio.userId = "0928";
        Emilio.email = "Cubano@gmail.com";
        Emilio.lastFourdigits = "0706";

        SearchClass Ulysses = new SearchClass();
        Ulysses.userId = "0914";
        Ulysses.email = "lysses@gmail.com";
        Ulysses.lastFourdigits = "01zx";

        var list = new List<SearchClass>();
        list.Add(Alex);
        list.Add(Emilio);
        list.Add(Ulysses);


        IEnumerable<SearchClass> result = list;
        if (!string.Empty.Equals(userId))
            result = result.Where(u => u.userId == userId);

        return result.ToList();
    }

2 个答案:

答案 0 :(得分:1)

首先,在您的控制器中,您正在创建一个searchClass:

List<AAlexUsers.Models.SearchClass> searchClass = AAlexUsers.Models.SearchClass.Users(userId,email,lastFourdigits);   

在您的Users(...)方法中,以下行始终返回非空的List。

result = result.Where(u => u.userId == userId);

return result.ToList();

如果result为null,result.ToList()将返回一个包含0个项目的List。 收到列表后,您就可以在控制器的这一行上分配它:

ViewBag.searchClass = searchClass;

在您看来,您正在检查:

<% if (ViewBag.searchClass !=null)

因为返回一个空列表,所以它总是为真。

如果您想根据发布的代码查看是否返回了正确的结果,则不应仅依赖于列表是否为空。相反,你应该这样做:

<% if (ViewBag.searchClass !=null && ViewBag.searchClass.Count > 0)

答案 1 :(得分:0)

所以你有这个代码:

公共ActionResult搜索() {     string userId = Request [“UserId”];     string email =请求[“电子邮件”] ??的String.Empty;     string lastFourdigits = Request [“LastFourDigits”] ??的String.Empty;

bool view = false;

if (string.IsNullOrEmpty(userId))
{
    view = true;
}
if (!view)
{
    List<AAlexUsers.Models.SearchClass> searchClass = 
        AAlexUsers.Models.SearchClass.Users(userId,email,lastFourdigits);
    {
        ViewBag.searchClass = searchClass;
        ViewBag.lastFourdigits = lastFourdigits;
        ViewBag.userId = userId;
        ViewBag.email = email;
    }
}
return View();

}

为控制器。您说您输入的用户无效,这意味着输入了用户名,因此,由于userId不为null或为空,因此视图将为“false”。

由于我们采用与视图相反的方式,这使我们成为真,然后执行获取Users方法返回的SearchClass对象列表的代码。现在,根据用户返回的内容(未给出),您可能返回一个空列表(new List())而不是null,这意味着当您将searchClass设置为ViewBag.searchClass时,您将永远不要将它设置为null。