dimanche 27 décembre 2015

Why do I get no random number?

I try to get this code to run. The code should generate me a integer number from 1 to 99999999 but always when I run it it only responds with 0. Could someone please tell me why?

  public ActionResult Edit([Bind(Include="AccountId,Balance,AccountType,AppUserId")] 
  Account   account)
    {
        if (ModelState.IsValid)
        {
            //account.AppUserId = User.Identity.GetUserId();
            Random rand = new Random();
            int bankId = 0;
            int i = 0;
            do
            {
                bankId = rand.Next(1, 100000000);
                if ((db.Accounts.Find(bankId)) == null)
                {
                    i = 1;
                    account.AccountId = bankId;
                }
                else
                {
                    i = 0;
                }
            } while (i == 0);                
            db.Accounts.Add(account);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.AccountType = new SelectList(db.AccountTypes, "AccountTypeId", "AccType", account.AccountType);
        return View(account);
    }




Aucun commentaire:

Enregistrer un commentaire