我有一個包含@ Html.DropDownListFor的視圖。當表單/視圖加載時,如果其中一個模型屬性具有值(IEnumerable),那麼它將創建一堆帶有相應數據的div。如果該屬性沒有任何值(又名Count()== 0),那麼它應該在窗體上顯示一個按鈕(這將爲該屬性創建數據)。因此,當用戶從下拉列表中選擇其中一個選項時,我會調用ajax調用與填充當前表單/視圖完全相同的動作方法,但是這次它會在id字段中發送一個值。重新加載視圖不會更新表格上的所有控件
我在我的操作方法中有一個斷點,並且驗證它正在被擊中,並且它具有正確的參數值併爲傳遞給該視圖的模型創建了正確的數據,但是......當模型被髮送到視圖以重新填充,表單上的項目/控件中沒有NONE會更改。我甚至在cshtml文件中加入了斷點,並且它還通過正確的數據。
所以,這裏是我的控制器:
public ActionResult Index(int? id)
{
var seasonId = id;
if (seasonId == null)
{
var player = _playerRepository.Query().FirstOrDefault(p => p.PlayerId == _userIdentity.PlayerId);
if (player.DefaultSeasonId != null)
seasonId = (int)player.DefaultSeasonId;
else
{
return View(new ScheduleModel
{
Player = player,
AvailableSeasons = _seasonRepository.Query().Select(s => s)
});
}
}
return View(CreateScheduleModelForSeason((int)seasonId));
}
這是我的觀點的開頭:
@model LeagueManager.Models.ScheduleModel
@{
ViewBag.Title = "Schedule(s)";
}
<div class="row">
@Html.LabelFor(m => m.AvailableSeasons)
@Html.DropDownListFor(m => m.SelectedSeasonId, new SelectList(Model.AvailableSeasons, "SeasonId", "SeasonName"), new { id = "seasonSelect" })
</div>
<form method="post" action="Schedule/GenerateSchedule">
<h2>The Season's Schedules/Weeks and Matchups</h2>
<div>
<div>
@if (Model.SchedulesAndMatches == null || (Model.SchedulesAndMatches != null && !Model.SchedulesAndMatches.Any()))
{
<input type="submit" class="btn btn-primary" value="Generate Schedule" />
}
</div>
和這裏的Ajax調用:
@* Season Selector *@
$('select#seasonSelect').change(function() {
var selectedSeasonId = $(this).val();
$.ajax({
url: '/Schedule/Index',
data: { id: selectedSeasonId }
});
});
同樣,所有實際的代碼正在工作,它只是不重新渲染視圖...
示例:調用id = 1的ActionResult方法時,它會加載整個計劃。當通過下拉菜單切換到id = 2(然後通過ajax再次調用)時,它保持相同的時間表。
另一方面:當調用id = 2的ActionResult方法時,它加載單個按鈕。當通過下拉菜單切換到id = 1時,它將重新填充模型中的正確數據,但視圖/表單不反映新信息。
請幫忙!
能否請你解釋進一步需要究竟是什麼?當您從下拉列表中選擇一個選項時,是否要更新下拉列表? – Haritha