我有2个DropDownList,第一个是Project,第二个是Users。项目没有直接列用户但有组列,在用户中也有组列。所以我的查询是这样的,
public ActionResult Create(string value){
//This is to shows ProjectList In Dropdown
var list = db.Projects.ToList().Select(x => x.Name);
IEnumerable<string> ProjectName;
ProjectName = list;
ViewBag.ProjectName = ProjectName;
// This is to show UsersList in Dropdown
var group = db.Projects.Where(x => x.Name == value).Select(y => y.GroupDev).FirstOrDefault();
string NameGroup = group;
var listuser = db.UsersModels.ToList().Where(x => x.GroupName == NameGroup).Select(y => y.Email);
IEnumerable<string> TaskUser;
TaskUser = listuser;
ViewBag.Users = TaskUser;
return View();
}
所以我的工作流程将是这样的,首先我从Dropdown中选择The ProjectName,然后我将使用JQuery从我的DrowpDon获取值,然后根据ProjectName选择什么是groupname。然后我创建IEnumerable以显示与GroupName匹配的所有用户。现在我不知道为什么它不起作用,这是因为我的逻辑错了?或者我的Jquery错了? 这是我的Jquery,
$(document).ready(function () {
$('#Project').change(function () {
var self = $(this);
var value = $('#Project option:selected').text();
$.ajax({
url: '/TaskModels/Create',
data: {
value: value
},
type: 'POST',
});
});
});
这是我的Dropdown,
<div class="form-group">
@Html.LabelFor(model => model.Project, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.Project, new SelectList(ViewBag.ProjectName, "Project"), "Pilih Project ")
@Html.ValidationMessageFor(model => model.Project, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.User, new SelectList(ViewBag.Users, "User"), "Pilih User")
@Html.ValidationMessageFor(model => model.User, "", new { @class = "text-danger" })
</div>
</div>
我已经在我的脚本部分
中添加了我的脚本@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script src="~/Custom_Script/DropDownListenere.js"></script>
}
任何人都可以帮助我们吗?我假设这是因为我的jquery,因为我不熟悉那种语言。
答案 0 :(得分:1)
再创建一个操作(例如:GetUser()),它接受一个参数(选定的Project)并将用户列表作为JSON返回。然后将项目的更改事件更改为:
$(document).ready(function () {
$('#Project').change(function () {
var self = $(this);
var value = $('#Project option:selected').text();
$.ajax({
url: '/TaskModels/GetUser', //Call GetUser Action instead of Create.
data: {
value: value
},
type: 'POST',
success:function(data){ //add this success function
$("#User").html(""); //To clear the existing options.
data.forEach(function (user) { //Iterate through the output array (In this case list of user emails)
$("#User").append($("<option></option>") //append options to User drop downs
.attr("value",user)
.text(user))
})
}
});
});
});
答案 1 :(得分:0)
U可以使用来自具有id GroupName的服务器响应的Append users结果示例:$('#Project')。change(function(){var self = $(this); var value = $('#Project option:选中')。text(); $ .ajax({url:'/ TaskModels / Create',data:{value:value},type:'POST',success:function(data){$ .each(items,function) (i,item){$('#mySelect')。append($('',{value:item.id,text:item.username}));});}});});