2011-11-24 56 views
0

我用Razor ViewEngine使用MVC3应用程序。我有一个有几个RadioButtons和一个Select列表框的表单。在选定的单选按钮的基础上,我从控制器获取区域列表。这个列表然后被用来填充选择列表,把Title作为Value和ID作为ID。我遇到的问题是我的选择列表显示[object] [Object],而不是Area Name。无法通过JQuery填充选择列表。环境:MVC3 Razor

<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 

请指导,我在做什么错? 在此先感谢。

回答

1

尝试

success: function (result) { 
    $('#SelectionValues').empty();    
    $.each(result, function (index, elem) { 
     $('#SelectionValues').append(
      $("<option/>").attr("value", elem.ID) 
          .text(elem.Title) 
        ); 
       }); 
       } 

还设置了dataType:'json'所以你收到JSON解析

+0

您好,感谢,上面的文字是行不通的。 – user1001493

+0

你的'结果'是怎么样的,你有没有设置dataType:'json'? –

+0

没有伴侣,我是新来的这些东西。控制器方法已在上面发布。问候 – user1001493