¡Hola!鉤到javascript下拉列表更改
我目前的任務是建立一個頁面,在下拉列表中,用戶可以選擇一個甲板標題。一旦選擇了標題,該頁面應該在該組中的細節上回傳。
下面是我在此刻得到了:
@model IEnumerable<SCATChartsMVC.Models.Charts_DeckList>
@{
ViewBag.Title = "Index";
if (IsPost) { ViewBag.Title = "We posted back!"; }
}
<h2>Index</h2>
@{ var list = ViewData.Model.Select(cl => new SelectListItem
{
Value = cl.RecNum.ToString(),
Text = cl.DeckTitle.ToString()
});
}
@using (Html.BeginForm("Details", "Charts_DeckList", FormMethod.Post))
{
@Html.DropDownList("deckTitles", list, "---------select---------")
<input type="submit" name="submit" value="Submit" />
@Html.ActionLink("Details", "Details", "Charts_DeckList", new { id = list.ElementAt(4).Text }, "")
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$("deckTitles").change(function() {
if ($("#deckTitles").val() != "") {
var test = {};
test.url = "/Charts_DeckList/Details";
test.type = "POST";
test.data = JSON.stringify($('#deckTitles').val());
test.datatype = "json";
test.contentType = "application/json";
test.success = true;
test.error = function() { alert("Error!"); };
$.ajax(test);
}
})
</script>
輸入標籤和Html.BeginForm下ActionLink的是我自己的測試目的;如果指定元素,ActionLink將正常工作。我希望能夠在用戶點擊選擇時傳回類似的內容,而不是每當他們點擊「詳細信息」按鈕。
提交輸入標記不起作用。它確實路由到Charts_DeckList/Details,但操作中的參數始終爲空。
我剛進入整個MVC/Web rigamarole,所以有很多我不知道我甚至不知道我不知道。雖然我在互聯網上看到了許多不同的資源,提出了不同的建議,但在這個時候,大部分的網絡開發術語都在我身上丟失了,而這些事情在很多情況下都是失敗的,因爲VS似乎自動地將它放在一起。
任何指針,將不勝感激。謝謝。
巴里克的建議以下是正確的!
我還必須將腳本標記上移到BeginForm括號中。
你的問題是不動的腳本標記。它建議他們在底部。至於腳本,這行$(「deckTitles」)。change(function()'應該是'$(「#deckTitles」)。change(function()'至於提交不工作,你需要發佈你動作方法 – 2014-08-27 22:42:06