【发布时间】:2016-12-26 07:09:15
【问题描述】:
我的下拉列表数据正在视图中使用 ajax 助手传递给控制器,在控制器中,部分视图使用这些数据返回。当我运行应用程序以查看局部视图时,它会引发错误:
EntityFramework.SqlServer.dll 中出现“System.Data.Entity.Core.EntityCommandExecutionException”类型的异常,但未在用户代码中处理
但是,当我慢慢调试时,页面中正确显示了部分视图。 我假设由于在调试模式下正确显示了部分视图,我可能需要延迟对 ajax 的响应?这是正确的方法吗?有什么想法吗?
查看:
@using (Ajax.BeginForm("GetEmployee", "Employee", new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "showResult"
}))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-md-2" for="CountryId">Country</label>
<div class="col-md-10">
@Html.DropDownList("CountryId", null, "-- Select Country --", new { @class = "form-control", @onchange = "FillCity()" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="City">City</label>
<div class="col-md-10">
@Html.DropDownListFor(m => m.City, new SelectList(Enumerable.Empty<SelectListItem>(), "CityId", "CityName"), "-- Select City --", new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Search" class="btn btn-default" />
</div>
</div>
</div>
}
<div id="showResult"></div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script>
function FillCity() {
var countryId = $('#CountryId').val();
$.ajax({
url: '/Posting/FillCity',
type: "GET",
dataType: "JSON",
data: { country: countryId },
success: function (cities) {
$("#City").html(""); // clear before appending new list
$("#City").append($('<option>-- Select City --</option>'))
$.each(cities, function (i, city) {
$("#City").append($('<option></option>').val(city.CityId).html(city.CityName));
});
}
});
}
</script>
}
控制器:
[HttpPost]
public ActionResult GetEmployee(int CountryId, int city)
{
var model = db.Employees
.Where(x => x.Country.CountryId == CountryId && x.City.CityId == city);
return PartialView("PartialEmployee", model);
}
部分视图:
@model IEnumerable<PokeGoTradeModel.Models.Employee>
<table class="table">
<tr>
<th>Country</th>
<th>City</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Country.CountryName)</td>
<td>@Html.DisplayFor(modelItem => item.City.CityName)</td>
</tr>
}
</table>
这是我的内部异常堆栈跟踪:
at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
【问题讨论】:
-
不,这不是问题。内部异常详细信息是什么?
-
延迟 ajax 的可怕方法。您确定在调试时将相同的值发送到您的控制器吗?
-
@StephenMuecke 所以内部异常说“{”已经有一个打开的 DataReader 与此命令关联,必须先关闭。”}”
-
这就是问题所在:)。但是您没有显示相关代码或指出哪一行引发了该异常。
-
@monstertjie_za 是的,他们正在发送相同的值。顺便说一句,您不会相信,因为当我处于调试模式并在为 foreach 循环设置断点的局部视图中快速按 f11 时,它会引发该错误,但如果我等待几秒钟并缓慢按 f11,值正确输入,因此它正确呈现在视图中。
标签: ajax asp.net-mvc asp.net-mvc-partialview