Asp.net mvc 3 无法通过JQuery填充选择列表。环境:MVC3剃须刀
我正在使用MVC3应用程序和Razor ViewEngine。我有一个带有几个单选按钮和一个选择列表框的表单。根据选中的单选按钮,我从控制器获得一个区域列表。然后,通过将标题作为值,ID作为ID,使用此列表填充选择列表。我遇到的问题是,我的选择列表显示的是[object][object],而不是区域名称Asp.net mvc 3 无法通过JQuery填充选择列表。环境:MVC3剃须刀,asp.net-mvc-3,jquery,jquery-selectors,razor,Asp.net Mvc 3,Jquery,Jquery Selectors,Razor,我正在使用MVC3应用程序和Razor ViewEngine。我有一个带有几个单选按钮和一个选择列表框的表单。根据选中的单选按钮,我从控制器获得一个区域列表。然后,通过将标题作为值,ID作为ID,使用此列表填充选择列表。我遇到的问题是,我的选择列表显示的是[object][object],而不是区域名称 <div> @Using Html.BeginForm() @<fieldset> <div class="editor-field">
<div>
@Using Html.BeginForm()
@<fieldset>
<div class="editor-field">
@Html.Label("Airport")
@Html.RadioButton("Opt1", "AIR", True)
</div>
<div class="editor-field">
@Html.Label("Seaport")
@Html.RadioButton("Opt1", "SEA", False)
</div>
<div class="editor-field">
@Html.Label("Hotel")
@Html.RadioButton("Opt1", "HOT", False)
</div>
<div class="editor-field">
@Html.Label("Postcode")
@Html.RadioButton("Opt1", "PC", False)
</div>
<select id="SelectionValues" name="SelectedValue" size="width: 100px">
<option>
</option>
</select>
</fieldset>
End Using
<script type="text/javascript">
$(document).ready(function () {
$(":checkbox, :radio").click(function () {
var type = $('form input[type=radio]:checked').val();
$.ajax({
url: '/Home/GetAreasByType',
type: 'POST',
data: { type: type },
success: function (result) {
$('#SelectionValues').empty();
for (var i = 0; i < result.length; i++) {
var opt = new option(result[0, i], result[1, i]);
$('#SelectionValues').append(opt);
}
}
});
});
});
</script>
在存储库方面,我有以下几点:
Public Class AreaRepository
Implements IAreaRepository
Private _areas As New List(Of Area)
Sub New()
_areas.Add(New Area With {.ID = "SE18 6HX", .Title = "SE18 6HX", .Type = "PC"})
_areas.Add(New Area With {.ID = "SE8 4AF", .Title = "SE8 4AF", .Type = "PC"})
_areas.Add(New Area With {.ID = "RH6 8RJ", .Title = "RH6 8RJ", .Type = "PC"})
_areas.Add(New Area With {.ID = "EC1 4AF", .Title = "EC1 - Westminister", .Type = "PC"})
_areas.Add(New Area With {.ID = "Hot-1", .Title = "Holiday Inn Express", .Type = "HOT"})
_areas.Add(New Area With {.ID = "Hot-2", .Title = "IBIS Hotel", .Type = "HOT"})
_areas.Add(New Area With {.ID = "Hot-3", .Title = "Marriot Hotel", .Type = "HOT"})
_areas.Add(New Area With {.ID = "Hot-4", .Title = "Shariton", .Type = "HOT"})
_areas.Add(New Area With {.ID = "Sea-1", .Title = "Dover", .Type = "SEA"})
_areas.Add(New Area With {.ID = "Sea-2", .Title = "Portsmouth", .Type = "SEA"})
_areas.Add(New Area With {.ID = "Sea-3", .Title = "Plymouth", .Type = "SEA"})
_areas.Add(New Area With {.ID = "Air-1", .Title = "Gatwick", .Type = "AIR"})
_areas.Add(New Area With {.ID = "Air-2", .Title = "Heathrow", .Type = "AIR"})
_areas.Add(New Area With {.ID = "Air-3", .Title = "Luton", .Type = "AIR"})
End Sub
Public Function GetAreaByType(Type As String) As System.Collections.Generic.List(Of Area) Implements IAreaRepository.GetAreaByType
Dim var = (From a In _areas
Order By a.Title Ascending
Where a.Type = Type
Select a).ToList()
Return var
End Function
End Class
请引导,我做错了什么?
提前谢谢。试试看
success: function (result) {
$('#SelectionValues').empty();
$.each(result, function (index, elem) {
$('#SelectionValues').append(
$("<option/>").attr("value", elem.ID)
.text(elem.Title)
);
});
}
还要设置数据类型:'json',这样你收到的json就被解析了你的结果是什么样子的,你是否设置了数据类型:'json'?不,伙计,我对这些东西不熟悉。控制器方法张贴在上面。当做
Public Class AreaRepository
Implements IAreaRepository
Private _areas As New List(Of Area)
Sub New()
_areas.Add(New Area With {.ID = "SE18 6HX", .Title = "SE18 6HX", .Type = "PC"})
_areas.Add(New Area With {.ID = "SE8 4AF", .Title = "SE8 4AF", .Type = "PC"})
_areas.Add(New Area With {.ID = "RH6 8RJ", .Title = "RH6 8RJ", .Type = "PC"})
_areas.Add(New Area With {.ID = "EC1 4AF", .Title = "EC1 - Westminister", .Type = "PC"})
_areas.Add(New Area With {.ID = "Hot-1", .Title = "Holiday Inn Express", .Type = "HOT"})
_areas.Add(New Area With {.ID = "Hot-2", .Title = "IBIS Hotel", .Type = "HOT"})
_areas.Add(New Area With {.ID = "Hot-3", .Title = "Marriot Hotel", .Type = "HOT"})
_areas.Add(New Area With {.ID = "Hot-4", .Title = "Shariton", .Type = "HOT"})
_areas.Add(New Area With {.ID = "Sea-1", .Title = "Dover", .Type = "SEA"})
_areas.Add(New Area With {.ID = "Sea-2", .Title = "Portsmouth", .Type = "SEA"})
_areas.Add(New Area With {.ID = "Sea-3", .Title = "Plymouth", .Type = "SEA"})
_areas.Add(New Area With {.ID = "Air-1", .Title = "Gatwick", .Type = "AIR"})
_areas.Add(New Area With {.ID = "Air-2", .Title = "Heathrow", .Type = "AIR"})
_areas.Add(New Area With {.ID = "Air-3", .Title = "Luton", .Type = "AIR"})
End Sub
Public Function GetAreaByType(Type As String) As System.Collections.Generic.List(Of Area) Implements IAreaRepository.GetAreaByType
Dim var = (From a In _areas
Order By a.Title Ascending
Where a.Type = Type
Select a).ToList()
Return var
End Function
End Class
success: function (result) {
$('#SelectionValues').empty();
$.each(result, function (index, elem) {
$('#SelectionValues').append(
$("<option/>").attr("value", elem.ID)
.text(elem.Title)
);
});
}