[HttpPost]
public async Task<IActionResult> Register(ViewModel model,AppUser user )
{
var app = db.AppUsers.Where(s => s.Name.Equals(s.Name)).ToList();
ViewBag.App = app;
if (!ModelState.IsValid)
{
return View();
}
//this is the registration table with RegistrationId as pk
Registration registration = new Registration
{
FirstName = model.Registration.FirstName,
LastName = model.Registration.LastName,
EmailAddress = model.Registration.EmailAddress,
PhoneNumber = model.Registration.PhoneNumber,
Address = model.Registration.Address,
Gender = model.Registration.Gender,
RegistrationId = model.Registration.RegistrationId
};
db.Registration.Add(registration);
await db.SaveChangesAsync();
//here i'm passing the RegistrationId into an int that will be used as a foreign key in other tables
int latestId = registration.RegistrationId;
this.RegId = latestId;
//this is the activity log table that have the RegistrationId as FK
ActivityLog log = new ActivityLog
{
Purpose = model.Log.Purpose,
NameOfCompany = model.Log.NameOfCompany,
ToWhom = model.Log.ToWhom,
Login = model.Log.Login = DateTime.Now,
//here i implemented the latestId that was created above
RegistrationId = latestId
};
db.Logs.Add(log);
registration.RegistrationId = log.RegistrationId = latestId;
await db.SaveChangesAsync();
// model.Registration = registration;
//return View("Capture", model);
//i did a redirect to action to pass the id to another view
return RedirectToAction("Capture", "Registration", new {id = latestId});
}
public IActionResult Capture(string Id)
{
return View();
}
[HttpPost]
public async Task<IActionResult> Capture(ViewModel model,string name)
{
//i want to use the latestId from the Register method on this Capture method
int id = this.RegId;
var files = HttpContext.Request.Form.Files;
// Registration registration= db.Registration.Where(s => s.RegistrationId == s.RegistrationId).FirstOrDefault();
//int id = registration.RegistrationId;
if (files != null)
{
foreach (var file in files)
{
if (file.Length > 0)
{
//Getting fileName
var fileName = file.FileName;
//Unique filename "Guid"
var myUniqueFileName = Convert.ToString(Guid.NewGuid());
//Getting Extension
var fileExtension = Path.GetExtension(fileName);
//Concatinating filename +fileExtension(Unique filename)
var newFileName = string.Concat(myUniqueFileName, fileExtension);
//Generating path to store photos
var filePath = Path.Combine(hostingEnvironment.WebRootPath, "CameraPhotos") + $@"\{newFileName}";
if (!String.IsNullOrEmpty(filePath))
{
//Storing Image in folder
StoreInFolder(file, filePath);
}
db.Bios.Add(new BioEntity
{
BioId = 0,
Date = DateTime.Now,
ImageBase64String = filePath,
RegistrationId = id
});
await db.SaveChangesAsync();
//var imageBytes = System.IO.File.ReadAllBytes(filePath);
//if (imageBytes != null)
//{
// try
// {
// if (imageBytes != null)
// {
// StoreInDatabase(imageBytes);
// }
// }
// catch (Exception)
// {
// throw;
// }
//}
}
}
}
else
{
}
return View();
}
/// <summary>
// /// Saving captured image into Folder.
// /// </summary>
// /// <param name="file"></param>
// /// <param name="fileName"></param>
private void StoreInFolder(IFormFile file, string fileName)
{
using (FileStream fs = System.IO.File.Create(fileName))
{
file.CopyTo(fs);
fs.Flush();
}
}
我有一个注册表和一个为我的应用程序保存图像的表。我需要将当前用户的注册ID从注册方法传递到图像捕获方法。该代码有效,但是一旦通过返回RedirectToAction(“ Capture”,“ Registration”,new {id = LatestId});有没有更好的方法可以做到这一点?
Note: I have the registration view and the image capturing view separately.