本文介绍了透明/不可见值附加在下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一段代码,使用getjson方法请求一些对象列表.控制器操作方法正确返回对象列表.以下代码将请求的数据追加到下拉列表中,但是它是不可见/透明的数据意味着它正在追加我的数据但不可见,即它是白色/透明的.这是代码:
I have a piece of code which requests some list of objects using getjson method. Controller action method returns list of objects properly. Following code appends requested data in drop down list, but it is invisible/transparent data means it is appending my data but it is not visible i.e. it white/transparent. Here is the code:
<script>
$(document).ready(function () {
$('#b1').click(function () {
var userName = "Hello"
$.getJSON("/classes/getCourseList?username=" + userName, function (data1) {
var myOptions =
{
val1: data1.title
};
var $mySelect = $('#s1');
$.each(myOptions, function (val, text) {
$mySelect.append($('<option />',
{
value: val,
text: text
}));
});
});
});
});
</script>
操作方法:
[AllowAnonymous]
public JsonResult getCourseList(string userName)
{
// Quiz q=new Quiz();
//q = _db.Quizzes.FirstOrDefault(x => x.QuizName.Equals(userName));
List<dummyCourses> list = new List<dummyCourses>();
foreach (Course c in db.Courses)
{
dummyCourses dc = new dummyCourses();
dc.title = c.title;
dc.creditHours = c.creditHours;
dc.instructor = c.instructor;
dc.code = c.code;
list.Add(dc);
}
return this.Json(list, JsonRequestBehavior.AllowGet);
}
推荐答案
您将获得如下结构:
[ { "title": "Title", ... }, ... ]
我的意思是对象数组,但是您在代码中使用了data1.title
(它将是undefined
).
I mean array of objects, but you use data1.title
(it will be undefined
) in your code.
必须为data1[ 0 ].title
,data1[ 1 ].title
等
这篇关于透明/不可见值附加在下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!