Asp.net mvc 如何基于另一个dropdownlist MVC(VB.Net)获取dropdownlist
我已经通过了一系列的教程来实现这个目标,但没有一个真正适合我。我在下面的链接中偶然发现了教程,并将其转换为我的VB.Net使用,这是唯一一个响应但出现错误的教程:Asp.net mvc 如何基于另一个dropdownlist MVC(VB.Net)获取dropdownlist,asp.net-mvc,vb.net,asp.net-mvc-5,Asp.net Mvc,Vb.net,Asp.net Mvc 5,我已经通过了一系列的教程来实现这个目标,但没有一个真正适合我。我在下面的链接中偶然发现了教程,并将其转换为我的VB.Net使用,这是唯一一个响应但出现错误的教程:[object]。不知道如何解决这个问题,有人能帮忙吗 下面是我的代码: 控制器 Public Function PullStates(ByVal id As Integer) As JsonResult Dim states = db.States.Where(Function(s) s.CountryCode = i
[object]
。不知道如何解决这个问题,有人能帮忙吗
下面是我的代码:
控制器
Public Function PullStates(ByVal id As Integer) As JsonResult
Dim states = db.States.Where(Function(s) s.CountryCode = id).[Select](Function(s) New With
{
Key .Text = s.Descrip,
Key .Value = s.Code
})
Return Json(New SelectList(states, "Value", "Text"))
End Function
'GET: Region/Create
Function Create() As ActionResult
ViewBag.Code = "AUTO"
ViewBag.Country = db.Countries.[Select](Function(c) New SelectListItem With
{
.Text = c.Descrip,
.Value = c.CountryCode
})
Return View()
End Function
查看
@Html.DropDownList("Country", CType(ViewBag.Country, IEnumerable(Of SelectListItem)), "--Select Country--", htmlAttributes:=New With {.class = "tb8"})
@Html.DropDownList("State", New SelectList(String.Empty, "Value", "Text"), "--Please Select State--", htmlAttributes:=New With {.class = "tb8"})
脚本
<!-- language: lang-js -->
<script src="~/Scripts/ddl/jquery-1.7.1.js" type="text/javascript"></script>
<script src="~/Scripts/ddl/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#Country").change(function () {
$("#State").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("PullStates")', // we are calling json method
dataType: 'json',
data: { id: $("#Country").val() },
success: function (states) {
// states contains the JSON formatted list
// of states passed from the controller
$.each(states, function (i, state) {
$("#State").append('<option value="' + state.Value + '">' + state.Text + '</option>');
}); // here we are adding option for States
},
error: function (ex) {
alert('Failed to retrieve states.' + ex);
}
});
return false;
})
});
</script>
<!-- end snippet -->
$(文档).ready(函数(){
//下拉列表Selectedchange事件
$(“#国家”).change(函数(){
$(“#State”).empty();
$.ajax({
键入:“POST”,
url:'@url.Action(“PullStates”),//我们正在调用json方法
数据类型:“json”,
数据:{id:$(“#Country”).val()},
成功:功能(状态){
//states包含JSON格式的列表
//从控制器传递的状态数
$.each(状态,函数(i,状态){
$(“#State”).append(“”+State.Text+“”);
});//这里我们为状态添加选项
},
错误:函数(ex){
警报(“检索状态失败”。+ex);
}
});
返回false;
})
});
我不知道vb.net,但在这里,您不需要将变量更改为选择列表,而是直接按如下方式传递
Public Function PullStates(ByVal id As Integer) As JsonResult
Dim states = db.States.Where(Function(s) s.CountryCode = id).[Select](Function(s) New With
{
Key .Text = s.Descrip,
Key .Value = s.Code
})
Return Json(states)
尝试这样更改ajax成功函数
success: function (data) {
$(data).each(function(){
$("#State").append('<option value="' + this.Value + '">' + this.Text + '</option>');
});
},
成功:函数(数据){
$(数据)。每个(函数(){
$(“#State”).append(“”+this.Text+“”);
});
},