在MVC5中使用Ajax.ActionLink加载部分视图

时间:2015-02-25 11:06:55

标签: jquery ajax visual-studio-2013

我试图在我的控制器中使用以下方法在Visual Studio中加载部分视图:

public ActionResult ListLocations(int SelectedYear, int continentId, int countryId, string search)
        {
            ViewBag.SchoolYear = SelectedYear;
            IEnumerable<ClimateChart> locationList = repository.FindLocationsByCountryID(continentId, countryId);
            if (!String.IsNullOrEmpty(search))
            {
                locationList = repository.FindLocationsByCountryID(continentId, countryId)
                    .Where(c => c.Location.ToLower().Contains(search));
            };

            LocationListViewModel model = new LocationListViewModel
            {
                Locations = locationList
            };

            if (Request.IsAjaxRequest())
            {
                return PartialView("NoLocationsInCountryErrorPartial");
            }
            else
            {
                return View(model);
            }
        }

这是html页面:

@model p2groep11.Net.ViewModels.CountryListViewModel

@{
    ViewBag.Title = "Countries";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Countries</h2>
<div id="partialdiv"></div>
<p>
    @using (Html.BeginForm("ListCountries", "Continent", FormMethod.Get))
    {
    <div>
        Land: @Html.TextBox("Search")
        <input type="submit" value="Filter" />
    </div>
    }
</p>

<div>
    @foreach (var item in Model.Countries)
    {
        <div>
            @Ajax.ActionLink(@item.Name, "ListLocations", new { Selectedyear = @ViewBag.SchoolYear, continentId = item.Continent.ContinentID, countryId = item.CountryID }, new AjaxOptions
               {
                   HttpMethod = "GET",
                   InsertionMode = InsertionMode.Replace,
                   UpdateTargetId = "partialdiv"
               })
        </div>       
    }
</div>

当我点击其中包含位置的国家/地区时,我会看到包含位置的页面。这按预期工作。 但是,当我点击没有位置的国家时,我应该留在页面上并收到错误(即部分视图)。现在我只是去那个国家的页面。

我怀疑Ajax.Actionlink有问题,但我无法弄清楚是什么。

我安装了Ajax不显眼的nuget包,并将脚本包含在布局文件中。

0 个答案:

没有答案