-1
我試着只刷新一個Html.ListBox。但我不知道如何用ASP來做到這一點。現在它將刷新整個頁面。如何在asp.net剃鬚刀中只刷新一個Html.ListBox?
這裏是我的源: 控制器
[HttpPost]
public ActionResult KenmerkSelectie(string Kenmerken1, string Buttontype, List<string> VarFromKenmerk1)
{
if (Buttontype == "submit")
{
//Write to a database and process stuff
return this.RedirectToAction("submitForm");
}else{
ViewBag.VarFromKenmerk1 = ...;
//TODO: Must refresh the ListBox: VarFromKenmerk1;
//AND AND NOT refresh whole page;
return View("index");
}
HTML視圖:
@using (Html.BeginForm("KenmerkSelectie", "KenmerkSelectie", FormMethod.Post, new { id = "kenmerk1" }))
{
@Html.DropDownList("Kenmerken1",
(SelectList)ViewBag.Kenmerk,
"-- Selecteer een kenmerk--", new
{
onchange = "refreshVarFromKenmerk1();"
})
@Html.ListBox("VarFromKenmerk1", (SelectList)ViewBag.var, new { @style = "width: 252px; height: 300px" })
<input type="submit" value="submit" id="btnNext" name="ButtonType" />
}
的Javascript:
<script type="text/javascript">
function refreshVarFromKenmerk1() {
document.getElementById('kenmerk1').submit();
}
</script>
是否有可能實現這一目標沒有JavaScript?或者更好,我該如何實現這一目標?
你想發送一個單獨的GET請求來填充選擇列表。不要試圖在同一控制器操作中同時執行「POST」和選擇列表填充。 (@beautifulcoder在他的回答中暗指了這一點。) – wahwahwah