如何延迟 ajax 响应 - asp.net mvc 使用局部视图
how to delay ajax response - asp.net mvc using partialview
我的下拉列表数据正在视图中使用 ajax 助手传递给控制器,在控制器中,部分视图使用这些数据返回。当我 运行 应用程序查看局部视图时,它抛出一个错误:
An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
但是,当我慢慢调试时,部分视图在页面中正确显示。
我假设由于部分视图在调试模式下按照我的预期正确显示,我可能需要延迟 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)
我明白了。这都是因为延迟加载。所以我的员工模型具有国家和城市的导航属性。在我的 Actionresult GetEmployee 方法中,我需要添加更多包含员工模型导航属性的行。例如,
var model = db.employees.Include(x=>x.Country)
.Include(x=>x.City)
.Where(x=>x.Country.CountryId=countryId && x.City.CityId=cityId);
希望对大家在使用ajax局部视图时遇到类似问题的朋友有所帮助。如果您在模型中使用虚拟 属性,例如:
public virtual Country Country { get; set; }
确保在查询中使用 Include。
我的下拉列表数据正在视图中使用 ajax 助手传递给控制器,在控制器中,部分视图使用这些数据返回。当我 运行 应用程序查看局部视图时,它抛出一个错误:
An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
但是,当我慢慢调试时,部分视图在页面中正确显示。 我假设由于部分视图在调试模式下按照我的预期正确显示,我可能需要延迟 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)
我明白了。这都是因为延迟加载。所以我的员工模型具有国家和城市的导航属性。在我的 Actionresult GetEmployee 方法中,我需要添加更多包含员工模型导航属性的行。例如,
var model = db.employees.Include(x=>x.Country)
.Include(x=>x.City)
.Where(x=>x.Country.CountryId=countryId && x.City.CityId=cityId);
希望对大家在使用ajax局部视图时遇到类似问题的朋友有所帮助。如果您在模型中使用虚拟 属性,例如:
public virtual Country Country { get; set; }
确保在查询中使用 Include。