我已经完成了一系列教程来实现这个目标,但没有一个对我有用。我在下面的链接中偶然发现了教程,并将其转换为我的VB.Net用法,并且这是唯一一个曾回复过但有错误的人:[object 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 -->
答案 0 :(得分:0)
我不知道vb.net但在这里你不需要将变量更改为选择列表jsut直接传递这样
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>');
});
},