Getting variable from cs file to the cshtml file in Razor Page
Can anyone help me with this? I want to retrieve the "file" variable from the code below, and use it in its cshtml file, or if possible, I want to save that variable to a field in the database (I already had a field "File Path" in its model)
Any help would be greatly appreciated!!
public CreateModel(Getaways.Data.ApplicationDbContext context,IHostingEnvironment environment ) { _context = context; _environment = environment; } [BindProperty] public IFormFile Upload { get; set; } public IActionResult OnGet() { PopulateCategoryDropDownList(_context); return Page(); } [BindProperty] public Post_info Post_info { get; set; } public async Task<IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return Page(); } var file = Path.Combine(_environment.ContentRootPath, "uploads", Upload.FileName); using (var fileStream = new FileStream(file, FileMode.Create)) { await Upload.CopyToAsync(fileStream); } var emptyPost = new Post_info(); if (await TryUpdateModelAsync<Post_info>( emptyPost, "post_info", // Prefix for form value. s => s.Post_ID, s => s.Category_ID, s => s.DatePosted, s => s.Rating, s => s.Description,s =>s.Country,s => s.City, s => s.Region, s => s.IdentityID)) { _context.Post_info.Add(emptyPost); await _context.SaveChangesAsync(); return RedirectToPage("./Index"); } PopulateCategoryDropDownList(_context, emptyPost.Category_ID); return Page(); } }
}
0 comments:
Post a Comment