工作這是我從視圖中的部分代碼稱爲index
asp.net的MVC不考慮
<div class="box-body">
@{ Html.RenderAction("_BlogsGrid",new {country=""});}
</div>
...
...
<script>
$("#SelectedCountry").change(function() {
var selectedCountry = $(this).val();
$.ajax({
url: '@Url.Action("_BlogsGrid","Blog")' + '?country=' + selectedCountry,
sucess: function(xhr, data) {
console.log('sucess');
},
error: function (err) {
console.log(err);
}
});
});
</script>
這裏是控制器動作代碼
public ActionResult _BlogsGrid(string country)
{
var blogs = _blogService.GetBlogWithCountryName(country).ToList();
var blogsList = BlogMapper.ToBlogIndex(blogs);
return PartialView(blogsList);
}
,這裏是_BlogsGrid視圖
@model Blog.BlogsList
<div class="pull-right">
@Html.DropDownListFor(m => m.SelectedCountry, new SelectList(Model.CountriesList), "Select a country", new { @class = "form-control" })
</div>
<br />
<br />
@if (Model.Blogs.Count == 0)
{
<div class="box-group">
<h4>Sorry we couldn't find any blog related to the country you asked for.</h4>
@Html.DisplayText("Hello world!")
</div>
}
else
{
<div class="box-group" id="accordion">
@foreach (var blog in Model.Blogs)
{
@*Some code*@
}
</div>
}
事情是當我第一次加載它everythi NG正常工作,控制器方法被擊中,所有的博客得到加載是這樣的觀點看起來如何
它碰到if條件(在視圖中放置一個斷點,檢查條件是否正在執行,否則條件正在執行,如果條件成立)(這是局部視圖) 但內容「如果「未在瀏覽器中加載。 我仍然收到與以前相同的內容。任何想法爲什麼我的內容沒有被更新?
更新:
<div id="grid" class="box-body">
@{ Html.RenderAction("_BlogsGrid",new {country=""});}
</div>
<script>
$("#SelectedCountry").change(function() {
var selectedCountry = $(this).val();
$.ajax({
url: '@Url.Action("_BlogsGrid","Blog")' + '?country=' + selectedCountry,
sucess: function (data) {
$('#grid').html(data);
},
error: function (err) {
console.log(err);
}
});
});
</script>
但仍然是我的div沒有更新。
我沒有看到任何代碼,將更新內容。你的ajax調用只是以'console.log'方式執行,不做任何事情來更新頁面。 –
你在ajax'sucess'回調裏面做什麼?這是這個職位或實際代碼的虛擬代碼嗎? – adiga
您的ajax函數會在id爲「SelectedCountry」的元素髮生變化時觸發,您忘記將其放入選擇標籤參數中,因此它應該像「@ Html.DropDownListFor(m => m.SelectedCountry,new SelectList Model.CountriesList),「選擇一個國家」,新的{@class =「form-control」,id =「SelectedCountry」})' – Tiramonium