2011-11-24 57 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