如何限制进入Asp.net MVC的客户数量

时间:2017-11-01 19:06:53

标签: c# asp.net

我正在尝试创建一个代码,当用户将五个客户输入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);
    }
}

1 个答案:

答案 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);
    }