2011-03-04 30 views
0

我有一個Dropdownlist連接到我的模型。加載頁面時,模型爲空,但在頁面上有一個用於更新模型的按鈕。我的問題是我的下拉列表不會更新。MVC更新使用jquery的下拉列表

標記:

@Html.DropDownList("ddlRaces", new SelectList(ViewBag.Races, "RaceId", "Name")) 
<input type="button" id="btnChangeRace" class="btnGo" value=" " /> 

@Html.DropDownListFor(m => m.Timers, new SelectList(Model.Timers, "TimerId", "StartTime"), "Velg timer:") 

腳本:

btnChangeRace.click(function() { 
    url = "/TimeMerger/GetTimers/?raceId=" + ddlRaces.val(); 
    $.get(url); 
}); 

代碼隱藏:

[HttpGet] 
public ActionResult GetTimers(int raceId) 
{ 
    var timeMergeModel = new TimeMergerModel(); 
    timeMergeModel.Timers = TimerModel.GetTimers(raceId); 
    return View(timeMergeModel); 
} 
+0

向我們展示btnChangeRace和ddlRaces的聲明。 – 2011-03-04 12:10:48

回答

0
$.get(url); 

這裏給您發送AJAX請求,但是你在成功回調無所事事所以沒有更新。甚至沒有成功的回調。所以你需要定義一個成功回調:

$.get(url, function(result) { 
    // result here will represent the value returned by the server 
    // which in your case is HTML. So here you might need to update 
    // the relevant portion of your page. 
}); 

另外你的DropDownListFor的定義是錯誤的。您正在使用與第一個和第二個參數(Model.Timers)相同的模型變量。這個幫助器的第一個參數應該是你要綁定的標量屬性,而不是一個列表。它應該是:

@Html.DropDownListFor(
    m => m.SelectedTimerValue, 
    new SelectList(Model.Timers, "TimerId", "StartTime"), 
    "Velg timer:" 
) 

其中SelectedTimerValue將是對您的視圖模型的屬性,將舉行選定值。此外,爲什麼你使用DropDownList而不是DropDownListFor爲第一個下拉?視圖模型的整個概念是它將包含所有必要的信息,以便視圖可以簡單地顯示它。

+0

thnx幫忙:) – Arnstein 2011-03-07 09:42:10