Asp.net mvc 如何在控制器操作中创建序列号?

Asp.net mvc 如何在控制器操作中创建序列号?,asp.net-mvc,database,Asp.net Mvc,Database,我正在从事一个项目,这是一个电子健康患者网关 我想根据从视图到控制器的提交表单生成一个数字(int类型)(当静脉采血员收到血样时,他将选中该框并单击提交按钮)。该编号是将在我的控制器中生成并保存在数据库中的患者血样编号。我使用下面的逻辑来创建数字,但它总是给我零 请让我知道我如何可以创建一个接一个的顺序和存储在数据库中的数字 public ActionResult AddSampleTest(int Pid, int Tid, int Stid, string Comment ,string

我正在从事一个项目,这是一个电子健康患者网关

我想根据从视图到控制器的提交表单生成一个数字(int类型)(当静脉采血员收到血样时,他将选中该框并单击提交按钮)。该编号是将在我的控制器中生成并保存在数据库中的患者血样编号。我使用下面的逻辑来创建数字,但它总是给我零

请让我知道我如何可以创建一个接一个的顺序和存储在数据库中的数字

public ActionResult AddSampleTest(int Pid, int Tid, int Stid, string   Comment ,string testDate,int DotorID)
    {
        int sampleNumber=1;
        Random rnd = new Random();
        int[] sample = new int[50000];
        rnd.Next();
        for (int ctr = 1; ctr <= sample.Length; ctr++)
        {
            sampleNumber=sample[ctr + 1];

        }




        string sampleno = sampleNumber.ToString();
        DateTime TestDate = Convert.ToDateTime(testDate);
        int  Recomended_Test_DoctorID = DotorID;

        // Update the Sample Status 
        PatientTest objpatientTest = db.PatientTests.Where(x => x.PatientID == Pid && x.Testid == Tid && x.SubTestId == Stid && x.Date==TestDate &&x.DoctorId== Recomended_Test_DoctorID).FirstOrDefault();
        objpatientTest.SampleStatus = "Sample Received";
        objpatientTest.SampleNumber = sampleno;
        db.SaveChanges();

        var name = User.Identity.Name;
        int Lid = db.LabAttendantRecords.Where(x => x.Name == name).Select(x => x.User_id).FirstOrDefault();



        LabTestSample obj = new LabTestSample();
            obj.labtestid = Tid;
            obj.PatientId = Pid;
            obj.subtestid = Stid;
            obj.labAtendid = Lid;
            obj.date = DateTime.Now.Date;
            obj.sampleReceived = true;


            obj.Comment = sampleno;
            db.LabTestSamples.Add(obj);
            db.SaveChanges();

        return RedirectToAction("TestSampleNumber", "LabAttendantDashboard", new { sampleno});

        // Use This for add Result
        return RedirectToAction("SubTest", "LabAttendantDashboard", new { Pid, Tid, Stid });
    }
public ActionResult AddSampleTest(int-Pid、int-Tid、int-Stid、string-Comment、string-testDate、int-DotorID)
{
int sampleNumber=1;
随机rnd=新随机();
int[]样本=新int[50000];
rnd.Next();
对于(int ctr=1;ctr x.PatientID==Pid&&x.Testid==Tid&&x.SubTestId==Stid&&x.Date==TestDate&&x.DoctorId==Recommend\U Test\U DoctorId)。FirstOrDefault();
objpatientTest.SampleStatus=“收到样本”;
objpatientTest.SampleNumber=sampleno;
db.SaveChanges();
var name=User.Identity.name;
int Lid=db.LabAttendantRecords.Where(x=>x.Name==Name).Select(x=>x.User\u id).FirstOrDefault();
LabTestSample obj=新LabTestSample();
obj.labtestid=Tid;
obj.PatientId=Pid;
obj.subtestid=Stid;
obj.labAtendid=盖子;
obj.date=DateTime.Now.date;
obj.sampleReceived=真;
obj.Comment=sampleno;
db.LabTestSamples.Add(obj);
db.SaveChanges();
返回RedirectToAction(“TestSampleNumber”,“LabAttendantDashboard”,new{sampleno});
//用于添加结果
返回重定向操作(“子测试”,“LabAttendantDashboard”,新{Pid,Tid,Stid});
}

sampleNumber
始终为0,因为
sample
中的每个元素都为0。 如果希望它是一个随机数,则需要执行以下操作:

Random rnd = new Random();
int sampleNumber = rnd.Next();
string sampleno = sampleNumber.ToString();
如果需要确保样本号是唯一的,则应使用Guid:

string sampleno = Guid.NewGuid().ToString();

在何处将随机数分配给变量?感谢SiHa,我在forThanks SiHa的末尾赋值,我在for循环的末尾赋值,您可以看到下面的字符串sampleno=sampleNumber.ToString();您的问题是sampleNumber=sample[ctr+1],它将始终为0。你不用随机值。谢谢Cubi,你的逻辑是创建随机数,但我认为创建的随机数可以是相同的。如何确保随机数与之前创建的随机数不同?每个患者的血样编号必须不同,不得重复。