ASP.Net 在操作方法 'SaveForm' 上保存表单导致 InvalidOperationException:未找到视图 'SaveForm'
ASP.Net Saving form on action method 'SaveForm' causes InvalidOperationException: The view 'SaveForm' was not found
更新: 因为它被保存了,我认为它被调用了两次,并且某处有代码正在执行此操作。可能与此有关:ASP.NET MVC Action is Called Twice
我检查了是否有多个 jquery & jquery 不显眼但找不到的实例。
我想知道为什么这段代码在调用操作方法时寻找视图 'SaveForm'?
它保存得很好(已验证数据库已更新),但它在后端抛出此错误(用户没有看到任何错误)。
这是我的表单标记:
<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();
}
}
}
错误信息:
它正在寻找视图 SaveForm.cshtml
出于某种原因:
我在 SaveForm
操作方法中尝试了不同的 return View()
,例如 return View("Success");
(我在同一文件夹中有 Success.cshtml
),我得到了这个:
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()
这是我的局部视图(它再次重新加载 _Layout 再次加载 jquery 再次调用操作)。在局部视图中需要这个:
@{
Layout = null;
}
更新: 因为它被保存了,我认为它被调用了两次,并且某处有代码正在执行此操作。可能与此有关:ASP.NET MVC Action is Called Twice 我检查了是否有多个 jquery & jquery 不显眼但找不到的实例。
我想知道为什么这段代码在调用操作方法时寻找视图 'SaveForm'?
它保存得很好(已验证数据库已更新),但它在后端抛出此错误(用户没有看到任何错误)。
这是我的表单标记:
<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();
}
}
}
错误信息:
它正在寻找视图 SaveForm.cshtml
出于某种原因:
我在 SaveForm
操作方法中尝试了不同的 return View()
,例如 return View("Success");
(我在同一文件夹中有 Success.cshtml
),我得到了这个:
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()
这是我的局部视图(它再次重新加载 _Layout 再次加载 jquery 再次调用操作)。在局部视图中需要这个:
@{
Layout = null;
}