2011-03-04 63 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