using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using EveryThing.Data; using EveryThing.Models; using EveryThing.Models.CodeTable; using Microsoft.AspNetCore.Authorization; namespace EveryThing.Pages.CodeTablePartners { [Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser,TransportThingUser")] public class EditModel : PageModel { private readonly ApplicationDbContext _context; public EditModel(ApplicationDbContext context) { _context = context; } [BindProperty] public CodeTablePartner Partner { get; set; } public async Task OnGetAsync(int? id) { if (id == null) { return NotFound(); } Partner = await _context.CodeTablePartners.Include(p => p.Country).FirstOrDefaultAsync(m => m.IdPartner == id); ViewData["IdPartner"] = id; if (Partner == null) { return NotFound(); } ViewData["IdCountryFk"] = new SelectList(_context.CodeTableCountries, "IdCountry", "TranslationSlovenian"); return Page(); } public async Task OnPostAsync() { if (!ModelState.IsValid) { return Page(); } var tempParter = _context.CodeTablePartners.Single(x => x.IdPartner == Partner.IdPartner); if (tempParter == null) { return NotFound(); } tempParter.Title = Partner.Title; tempParter.IdCountryFk = Partner.IdCountryFk; tempParter.City = Partner.City; tempParter.Street = Partner.Street; tempParter.HouseNumber = Partner.HouseNumber; tempParter.PostNumber = Partner.PostNumber; tempParter.Post = Partner.Post; tempParter.TaxNumber = Partner.TaxNumber; tempParter.RegistrationNumber = Partner.RegistrationNumber; tempParter.Supplier = Partner.Supplier; tempParter.Buyer = Partner.Buyer; tempParter.Active = Partner.Active; tempParter.Email = Partner.Email; //TODO: Mogoče //_context.Entry(newsPost).CurrentValues.SetValues(tempPartner); //_context.Attach(tempPartner).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PartnerExists(Partner.IdPartner)) { return NotFound(); } else { throw; } } return RedirectToPage("./Index"); } private bool PartnerExists(int id) { return _context.CodeTablePartners.Any(e => e.IdPartner == id); } } }