2012-09-26 45 views
0

.aspx的引擎無效的文本不打else語句

當我在文本框中輸入一個無效的用戶名和點擊搜索,「未找到類」是假設顯示。現在顯示「搜索結果」。它似乎沒有達到其他聲明。

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 = "[email protected]"; 
     Alex.lastFourdigits = "1885"; 

     SearchClass Emilio = new SearchClass(); 
     Emilio.userId = "0928"; 
     Emilio.email = "[email protected]"; 
     Emilio.lastFourdigits = "0706"; 

     SearchClass Ulysses = new SearchClass(); 
     Ulysses.userId = "0914"; 
     Ulysses.email = "[email protected]"; 
     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(); 
    } 

回答

1

首先,在你的控制器,你要創建一個searchClass:

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

在你的用戶(...)方法,你下面一行總是返回一個列表不爲空。

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

return result.ToList(); 

在這種結果的情況下爲空,result.ToList()將返回一個列表,在它0項。 一旦你收到你的列表,你再在這條線在你的控制器分配給它:

ViewBag.searchClass = searchClass; 

一旦在你看來,你檢查:

<% if (ViewBag.searchClass !=null) 

這將永遠是正確的因爲你返回一個空的列表。

如果您想查看是否根據您發佈的代碼返回了正確的結果,則不應只依賴列表爲空的情況。相反,你應該這樣做:

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

所以,你有這樣的代碼:

public ActionResult搜索() {user1d = Request [「UserId」]; string email = Request [「Email」] ??的String.Empty; string lastFourdigits =請求[「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不爲空或空,視圖將是「false」。

由於我們採取了與視圖相反的做法,它使我們真實,然後執行獲取由Users方法返回的SearchClass對象列表的代碼。現在,取決於用戶的回報是什麼(它沒有給出),你可能會返回一個空列表(新的List())而不是null,這意味着當你設置searchClass到ViewBag.searchClass時,你會永遠不要將它設置爲null。