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