Update: Because it gets saved, I'm thinking it got called twice, and there's code somewhere that's doing this. May be related to this: ASP.NET MVC Action is Called Twice I checked if there are multiple instances of jquery & jquery unobtrusive but can't find any yet.
I'm wondering why this code looking for view 'SaveForm' when it's just calling the action method?
It saves just fine (verified the database gets updated), but it throws this error on the backend (user doesn't see any errors).
Here's my form markup:
<form asp-controller="Profile" asp-action="SaveForm"
data-ajax="true"
data-ajax-method="POST"
data-ajax-update="#success"
>
<div class="panel panel-input-table panel-input-table-default panel-input-table-condensed">
<div class="panel-heading">
<h3 class="panel-title">Profile</h3>
</div>
<br />
<br />
<div class="panel-body ">
<div class="row">
@await Html.PartialAsync("_OrgStructureLevel", new OrgStructurePathModel { OrgStructureId = null })
</div>
</div>
<br />
<div id="success"></div>
<div class="panel-footer">
<button type="submit" name="btnSave" id="btnSave" class="btn btn-primary btn-save">Save</button>
<a name="btnReset" value="Reset" id="btnReset" href="javascript: location.reload()" class="btn btn-default btn-reset">
Reset
</a>
</div>
</div>
</form>
C#:
namespace IT.Web.Controllers
{
[BreadCrumb(Title = "Profile", UseDefaultRouteUrl = true, Order = 0)]
public partial class ProfileController : BaseController
{
private readonly ITContext _context;
public ProfileController(ITContext dbContext, IConfiguration config) : base(dbContext, config)
{
_context = dbContext;
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult SaveForm(int lastNodeIdSelect, int CapsUnit)
{
Profile profile = new Profile
{
UserId = _sessionUser.UserId,
CapsUnitId = CapsUnit,
NodeId = lastNodeIdSelect,
DepartmentId = _sessionUser.DepartmentId
};
using (var db = _context)
{
var userIdExist = (from P in db.Profiles where P.UserId == _sessionUser.UserId select P.UserId).Any();
if (userIdExist)
{
db.Set<Profile>().Update(profile);
db.SaveChanges();
}
else
{
db.Set<Profile>().Add(profile);
db.SaveChanges();
}
}
SessionUser sessionUser = new SessionUser();
sessionUser.CapsUnitId = CapsUnit;
sessionUser.NodeId = lastNodeIdSelect;
HttpContext.Session.Set<SessionUser>("SessionUser", sessionUser);
return View();
}
}
}
The error message:
It's looking for view SaveForm.cshtml
for some reason:
I've tried different return View()
in the SaveForm
action method such as return View("Success");
(I have Success.cshtml
in the same folder) and am getting this:
An unhandled exception occurred while processing the request. ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ITContext'. Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()