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>
<td>
<button class="searchButton" id="searchButton">Search</button>
</td>
</tr>
</table>
</form>
</div>
<hr />
<% if (ViewBag.searchClass !=null)
{ %>
<h2>Search Resuls</h2>
<br />
<%AAlexUsers.Models.SearchClass searchClassList= ViewBag.searchClass;%>
<table>
<tr>
<td>
UserID:
</td>
<td class="content">
<%=searchClassList.userId%>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td class="content">
<%=searchClassList.email%>
</td>
</tr>
<tr>
<td>
Last Four Digits:
</td>
<td class="content">
<%=searchClassList.lastFourdigits%>
</td>
</tr>
</table>
<%} else %>
<%{ %>
<h2>No Class found.</h2>
<%} %>
控制器
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Search()
{
string userId = Request["UserId"];
bool view = false;
if (Request["UserId"] == null)
{
view = true;
}
if (!view)
{
AAlexUsers.Models.SearchClass searchClass = new Models.SearchClass();
{
searchClass.lastFourdigits = "2222";
searchClass.userId = userId;
searchClass.email = "diaz@gmail.com";
string lastFourdigits = searchClass.lastFourdigits;
string userIdd = searchClass.userId;
string email = searchClass.email;
ViewBag.searchClass = searchClass;
ViewBag.lastFourdigits = lastFourdigits;
ViewBag.userId = userIdd;
ViewBag.email = email;
}
}
return View();
}
}
模型
public class SearchClass
{
public string userId { get; set; }
public string email { get; set; }
public string lastFourdigits { get; set; }
public SearchClass()
{
userId = "";
email = "";
lastFourdigits = "";
}
}