我正在使用剃刀ViewEngine的MVC3应用程序。我有一个带有少量RadioButtons和一个精选listBox的表单。在选定单选按钮的基础上,我从控制器获得了一个区域列表。然后这个列表被用来填充选择列表,将标题作为值,ID作为ID。问题是我的选择列表显示的是对象,而不是区域名称。
<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>在控制器端,我有以下函数:
<HttpPost()>
Function GetAreasByType(ByVal Type As String) As ActionResult
Debug.WriteLine(Type)
Dim areas = _areaRepository.GetAreaByType(Type)
Debug.WriteLine("========================================")
For Each a In areas
Debug.WriteLine(a.ID & " " & a.Title & " " & a.Type)
Next
Debug.WriteLine("========================================")
'Return Json(areas, JsonRequestBehavior.AllowGet)
Return Json(areas)
End Function注意:输出窗口给出了正确的结果。输出窗口的结果:
PC
========================================
EC1 4AF EC1 - Westminister PC
RH6 8RJ RH6 8RJ PC
SE18 6HX SE18 6HX PC
SE8 4AF SE8 4AF PC
========================================在存储库方面,我有以下几点:
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请指点一下,我哪里做错了?提前谢谢。
发布于 2011-11-25 01:01:42
试一试
success: function (result) {
$('#SelectionValues').empty();
$.each(result, function (index, elem) {
$('#SelectionValues').append(
$("<option/>").attr("value", elem.ID)
.text(elem.Title)
);
});
}还要设置dataType:'json',以便解析收到的json
https://stackoverflow.com/questions/8260259
复制相似问题