几天来我就是找不到问题本身,这真的快把我逼疯了。
我有 asp.net (mvc4) web 应用程序,其中有几个索引页面(显示列表),当单击列表中的任何项目时,它返回编辑 View 页面。 在编辑页面中(在 View 本身中)- 有一个提交按钮,它应该更新数据库并重定向到索引页面。
(起初,“提交”通过部分 View 加载了所有 html 编辑代码,但我更改了它以便我可以重定向到索引页面 - 因为“子操作不允许执行重定向操作”)。
所以问题是 Controller 没有重定向到索引页面。
当我在 Controller 的 post 函数中放置一个断点时,我看到很少有线程运行代码,尽管没有请求线程,如果我将光标放在其中一个进程调试箭头上,我可以看到消息“the进程或线程自上一步以来发生了变化”。
不知何故,我不知道如何,我在一页中解决了问题(不知道如何,因为我不知道是什么原因造成的),但有时(随机)它再次出现,而在另一页中我没有设法解决它。
这是来自 Controller 和 View 的一些代码:
Controller :
[HttpPost]
public ActionResult Edit([ModelBinder(typeof(OpportunityBinder))] OpportunityModel draft)
{
try
{
if (Request.Params["cancel"] != null)
{
return RedirectToAction("index", Utils.CrmDB.GetOpportunities());
}
if (draft.IsValid())
{
if (Utils.CrmDB.UpdateOpportunity(draft))
return RedirectToAction("Index", Utils.CrmDB.GetOpportunities());
}
return View(draft);
}
catch
{
return View();
}
}
查看:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
/* Some divs */
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
<fieldset>
/* Some partial views*/
/* loaded using @Html.Action() */
</fieldset>
}
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
}
部分 View 代码:
@model Genius_crm.Models.ActivityListModel
<p>
<button id="dlgAddActivity">
New
</button>
</p>
<div>
<table>
/* some tr and td */
</table>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#dlgAddActivity').each(function () {
var $link = $(this);
var pathname = window.location.pathname;
var parts = pathname.split('/')
var sub = parts[3]
var $dialog = $('<div id="addDialog"></div>')
.load('@Url.Action("AddActivityToOppPartial", "Opportunity")?CustId=@ViewBag.CustId&OppId=@ViewBag.OppId')
.dialog({
autoOpen: false,
title: $link.attr('title'),
width: 758,
height: 265,
resizable: false,
//close: function (event, ui) { window.location.reload();}
});
$link.click(function () {
$dialog.dialog('open');
return false;
});
});
});
所以 - 我希望我已经把我的问题说清楚了。 我浏览过一些关于这个主题的帖子,但没有一个对我有帮助。 这个问题也出现在 chrome 和 IE 中。
编辑 #1 - 当注释掉部分 View 时,问题在所有页面中都消失了!
编辑 #2 - 似乎在使用其他 Controller 操作的部分中加载的按钮存在问题。
编辑 #3 - 我添加了使用@Html.Action() 加载的局部 View 代码,其中包括一个用于 jquery-ui 对话框的脚本。
最佳答案
天哪!这是艰难的一周,但至少我吸取了教训。
问题由两部分组成:
(1) EF和DataContext管理(我重新打了题)。
我使用了 DataContext 的单个静态实例(单例),结果证明它是无效的并且有问题。
所以我在寻找一些信息,发现有更好的 DataContext 管理实现(您可以阅读 here),现在 - 一切似乎都正确!
(2) jQuery 验证表单提交时的所有部分。我必须找到一种方法来处理它。
只有一个部分而没有另一个部分的解决方案 - 没有产生好的结果。
关于c# - Controller 生成很少的线程,尽管没有被询问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22500720/