Aspx Engine
When I enter an invalid User Id in the text box and hit search, “No Class found” is suppose to be displayed. Now “Search Result” is displayed. It seems not to be hitting the else statement.
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>
<%} %>
Controller
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();
}
Model
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();
}
First of all, in your controller you are creating a searchClass:
In your Users(…) method you the following line always returns a List that is not null.
In the case that result is null, result.ToList() will return a List with 0 items in it.
Once you’ve received your List, you are then assigning it on this line in your controller:
Once in your view, you are checking for:
which will always be true since you return an empty List.
If you want to see if the proper result was returned, based on your posted code, you should not rely only on if the list is null. Instead you should do this: