如何延迟ajax响应 - 使用partialview的asp.net mvc

时间:2016-08-19 07:45:00

标签: ajax asp.net-mvc asp.net-mvc-partialview

我的下拉列表数据在视图和控制器中使用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);
}

PartialView:

@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)

1 个答案:

答案 0 :(得分:1)

我明白了。这完全是因为懒惰。所以我的员工模型具有国家和城市的导航属性。在我的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; }

确保在查询中使用 包含