问题描述
在我的控制器,我必须要显示在我看来类别列表中。这是报告我的应用程序的部分。
In my controller, I have this list of categories to be displayed in my view. This is for reporting section of my application.
public SelectList getFields()
{
List<SelectListItem> field = new List<SelectListItem>();
field.Add(new SelectListItem { Text = "Department", Value = "dept" });
field.Add(new SelectListItem { Text = "Referrer", Value = "ref" });
field.Add(new SelectListItem { Text = "Monthly", Value = "cd_key" });
return new SelectList(field, "Value", "Text");
}
public ActionResult HR_RecRep()
{
ViewBag.fields = getFields();
return View();
}
然后在我的观点:
Then in my View:
<script type="text/javascript">
$(document).ready(function () {
//this function will call the views with id=category
$('#fields').change(function () {
//Get the selected value from the dropdownlist
var rep = $(this).val();
//alert(cat);
if (rep.toUpperCase() == "DEPT") {
var text = document.getElementById("dept");
text.style.display = "inline";
$.getJSON('/Reports/getDepartment/', function (data) {
var items = '<option>Select Department...</option>';
$.each(data, function (i, department) {
items += "<option value='" + department.Value + "'>" + department.Text + "</option>";
});
$('#department').html(items);
});
var textValue = document.getElementById("ref");
textValue.style.display = "none";
}
});
});
</script>
<div style="border:0px solid black;width:950px;">
Reports By: @Html.DropDownList("fields", String.Empty)
<span id="dept" style="display:none;"><select id="department" name="department"></select></span>
<span id="ref" style="display:none;"><select id="referrer" name="referrer"></select></span>
在我看来,它成功地显示该报告通过部分的字段。
我想,只要我选择在其中指处的类别DropDownList的部门,它应该显示DropDownList的所有部门在数据库中。
In my view, it successfully displays the fields for the Reports By Section.I want that whenever I choose "dept" in the dropdownlist which refers to the Department in the Category, it should show the dropdownlist for all the departments in the database.
下面是我获取部门列表控制器。
Here's my controller for getting the list of department.
public SelectList getDepartment()
{
ViewBag.department = new SelectList(db.rms_departments, "dept_id", "dept_shortname");
return ViewBag;
}
当我试图用code,它不显示DropDownList中的部门时,我选择在报告的部门。并且还与推荐人,当我会从下拉菜单中选择它,它应该显示的下拉在数据库中的所有员工。
When I tried to use the code, it doesn't show the dropdownlist for the department when I choose department in the Reports. And also with the Referrer, when I will choose it from the dropdown, it should show the dropdown for all the employees in the database.
能否请您帮助我?
非常感谢。
Can you please assist me ?Thanks a lot.
也许有什么东西跟我的脚本?
Maybe there's something to do with my script?
推荐答案
您的问题是,你是不是从你的行动方法返回JSON,修改你的行动来回报JSON:
Your problem is that you are not returning JSON from your action method, modify your action to return JSON:
public ActionResult getDepartment()
{
var departments = db.rms_departments.Select(x=>
new
{
dept_id=dept_id,
dept_shortname=dept_shortname
});
return JSON(departments,JsonRequestBehavior.AllowGet);
}
和脚本修改行:
items += "<option value='" + department.Value + "'>" + department.Text + "</option>";
到
items += "<option value='" + department.dept_id+ "'>" + department.dept_shortname+ "</option>";
这篇关于显示基于所选择的下拉下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!