❔ Httppost Edit Action Update error
my httppost edit action does not store the form values.Shows an Update Sql exception regarding Country checkbox saving.
here is the view for Country checkboxes in Edit.cshtml:
@foreach(var country in new List<string> { "India", "America", "Australia", "China" })
{
<input type="checkbox" value="@country" id="@country" @(Model.Country.Contains(@country) ? "checked" : "")/>@country
<br/>
}
<span asp-validation-for="Country" class="text-danger"></span> </div> here is my httppost edit action method: [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(int id, RegistTable registration, IFormFile ProfileImage) { string wwwRootPath = _hostEnvironment.WebRootPath; if (id != registration.Id) { return NotFound(); } if (ModelState.IsValid) { // Get the selected country values from the form string[] countryValues = Request.Form["Country"]; var countries = string.Join(",", countryValues); registration.Country = countries; var fileName = Path.GetFileName(ProfileImage.FileName); var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images", fileName); using (var stream = new FileStream(path, FileMode.Create)) { ProfileImage.CopyTo(stream); } registration.ProfileImage = fileName; } try { _context.Update(registration); _context.SaveChanges(); } catch (DbUpdateConcurrencyException) { return NotFound(); } return RedirectToAction(nameof(Index);
<span asp-validation-for="Country" class="text-danger"></span> </div> here is my httppost edit action method: [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(int id, RegistTable registration, IFormFile ProfileImage) { string wwwRootPath = _hostEnvironment.WebRootPath; if (id != registration.Id) { return NotFound(); } if (ModelState.IsValid) { // Get the selected country values from the form string[] countryValues = Request.Form["Country"]; var countries = string.Join(",", countryValues); registration.Country = countries; var fileName = Path.GetFileName(ProfileImage.FileName); var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images", fileName); using (var stream = new FileStream(path, FileMode.Create)) { ProfileImage.CopyTo(stream); } registration.ProfileImage = fileName; } try { _context.Update(registration); _context.SaveChanges(); } catch (DbUpdateConcurrencyException) { return NotFound(); } return RedirectToAction(nameof(Index);
1 Reply
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.