我正在尝试创建一个代码,当用户将五个客户输入gridview时,会给他们一个错误,说他们应该只输入5个客户;现在发生的是用户可以任意数量的客户。我添加了错误行,但似乎我把它放错了地方,因为用户仍然可以添加超过5个。
for (int i = 0; i < getCustomer.items.Count; i++)
{
if (getIssue.name[i].idNumber == Customer.name.idNumber)
{
ViewBag.errorMessage = "ID number already exists";
return View(getCustomer);
}
if (getCustomer.items.Count >5)
{
viewBag.errorMessage=Error, You can only enter 5 customers at a time;
}
}
if (name.Count == 0)
{
nametbl.connectionString = ConfigurationManager.ConnectionStrings["CUSTOMERConnectionString"].ConnectionString;
try
{
name = nametbl.getName();
}
catch (Exception ex)
{
LogWrite logWriter = new LogWrite(ex.ToString());
ViewBag.errorMessage = "Unable to Connect to Database. Please see Log file for more Information";
return View(getCustomer);
}
}
答案 0 :(得分:0)
您只需要从命令流返回:
if (getCustomer.items.Count >5)
{
viewBag.errorMessage=Error, You can only enter 5 customers at a time; // <=== AFTER THIS LINE
return View(getCustomer);
}