我正在研究一個電子健康患者網關項目。如何在控制器操作中創建序列號?
我想根據提交表單從視圖生成一個數字(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 });
}
您在哪裏將隨機數賦值給變量? – PMerlet
感謝的SiHa,我在分配用於 –
感謝的SiHa最後,我在分配的for循環你可以看到下面串sampleno = sampleNumber.ToString()的結束; –