1
我的代碼中有一個從後端存儲庫填充的下拉列表。使用HTML5和存儲庫模式的級聯下拉列表
<h3>Upload Course Section Content</h3>
<div class="row">
<div class="nine columns">
<label for="name">Select Course:</label>
<select id="coursedd" name="courseid" style="height:40px; font-size:18px;">
<option value="0" id ="defaultcid" class ="choosefilter" >----Please Select Course----</option>
@foreach (var course in Model.GetCourseList())
{
<option value="@course.CourseID" id ="courseid" class ="choosefilter" >@course.Name </option>
}
</select>
</div>
</div>
<div class="row" style="margin-top:30px;">
<div class="nine columns">
<label for="name" id="namelabel">Select Course Section:</label>
<select id="coursesectiondd" name="coursesectionid" style="height:40px; font-size:18px;">
<option value="0" id ="defaultcs" class ="choosefilter" >----Please Select Course Section----</option>
@foreach (var courseSection in Model.GetCourseSectionsByCourseID(Model.CourseID))
{
<option value="@courseSection.CourseSectionID" id ="coursesectionid" class ="choosefilter" >@courseSection.Title </option>
}
</select>
</div>
</div>
第二個下拉菜單最初是隱藏的,當選擇第一個下拉菜單時,我希望填充下拉菜單。我曾嘗試使用下面的jQuery和JavaScript,但一直無法這樣做。任何人都可以請幫助我得到這個工作:
function GetCourseID() {
var id = document.getElementById("coursedd").value;
var postData = {
'CourseID': id
};
$.post('/Admin/GetCourseID/', postData, function (data) {
document.getElementById("coursedd").selectedIndex = id;
document.getElementByID("coursesectiondd").show();
});
};
$(function() {
$("#coursedd").change(function() {
$('#namelabel').show();
$('#title').show();
$('#CourseSectionSubmit').show();
var chosen = document.getElementById("coursedd").value;
if (chosen == "0") {
$('#namelabel').hide();
$('#coursesectiondd').hide();
$('#file').hide();
$('#filelabel').hide();
}
else {
$('#coursesectiondd').show()
GetCourseID()
$('#coursesectiondd').a
}
});
});
在我的控制器我有以下,我認爲這將更新適當的值的視圖模型來然後填充二級下拉列表,但正在顯示什麼。
[HttpPost]
public ActionResult GetCourseID(int courseID)
{
avm.CourseID = courseID;
return View(avm);
}
嗨達林,是有是在JavaScript中的錯誤,我已刪除。如果我在控制器中更改我的[HttpPost]動作以返回一個整數,那麼public int GetCourseID(int courseID){int id = courseID; return id;}我怎麼才能使用這個值刷新並填充第二個下拉菜單 – Jay
爲什麼你要改變這個動作返回一個整數?在ASP.NET MVC控制器中,動作必須返回ActionResults。在我的回答中,我建議你返回JSON(即JsonResult)。請參閱示例我已鏈接到n我的答案詳細說明:http://stackoverflow.com/a/4459084/29407 –
我不確定如何json的工作,一旦我有一個courseID整數,我可以成功地調用我的子部分if能夠刷新頁面,我認爲這可能是一個更簡單的解決方案,因爲我有一個方法來檢索課程部分基於課程的courseID下拉列表 – Jay