2011-03-21 117 views
0

這裏是我的jQuery:

$(document).ready(function() { 

    $("#GameId").change(function() { 

     $.get('/MatchManager/GetMatchType/' + $(this).val(), function (response) { 

      var Games = $.evalJSON(response); 

      var ddlSelectedProduct = $("#MatchTypeId"); 

      $("#MatchTypeId > option").remove(); 

      for (i = 0; i < Games.length; i++) { 

       ddlSelectedProduct.append($("<option />").val(Games[i].Value).text(Games[i].Text)); 

      } 
     }); 

    }); 

}); 

我打印出來的響應和正確的,但由於某種原因,我的程序停止在$.evalJson並說$.evalJSON is not a function這是我GetMatchType控制器只在情況下:

public string GetMatchType(int id) 
    { 
     var ListMatchTypes = new List<SelectListItem>(); 
     using (var db = new MatchGamingEntities()) 
     { 

      var MyMatchTypes = from m in db.MatchTypes 
           where m.GameId == id 
           select m; 
      foreach (var item in MyMatchTypes.ToList()) 
      { 
       ListMatchTypes.Add(new SelectListItem() { Text = item.MatchTypeName, Value = item.MatchTypeId.ToString() }); 
      } 
     } 
     return new JavaScriptSerializer().Serialize(ListMatchTypes); 

    } 

這是我的視圖:

@using(Html.BeginForm()){ @ Html.ValidationSummary(TRU E) MatchModel @ Html.LabelFor(型號=> model.GameId) @ Html.DropDownList( 「遊戲ID」,新的SelectList(ViewBag.MyGames如System.Collections.IEnumerable, 「遊戲ID」,「GameName 「), 」請選擇一個「)

</div> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.MatchTypeId) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("MatchTypeId", new SelectList(ViewBag.MatchTypes as System.Collections.IEnumerable, "Value", "Text")) 

    </div> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.MatchName) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.MatchName) 
     @Html.ValidationMessageFor(model => model.MatchName) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.MatchDescription) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.MatchDescription) 
     @Html.ValidationMessageFor(model => model.MatchDescription) 
    </div> 



    <div class="editor-label"> 
     @Html.LabelFor(model => model.Wager) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Wager) 
     @Html.ValidationMessageFor(model => model.Wager) <br /> 
     <span>Your Current Account Balance: @ViewBag.Balance</span> 
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 

}

@ Html.ActionLink(」 返回目錄」, 「索引」)
+0

有什麼不對$ .parseJSON? – 2011-03-21 21:16:32

回答

3

備註:

  • 控制器操作應始終返回ActionResults
  • 使用JsonResult,而不是與JavaScriptSerializer手動序列化
  • 你不需要任何$.evalJSON在客戶端手動解析=>如果您跟着我以前的評論,正確的內容類型application/json將被髮送,jQuery將自動解析傳遞給成功回調的對象。
  • 永遠不要在你的javascript中強調url =>在處理url時總是使用url helper,否則當你部署時,由於添加了虛擬目錄,你的代碼可能會中斷。

這是說讓我們試着提高你的代碼開始與控制器動作:

public ActionResult GetMatchType(int id) 
{ 
    using (var db = new MatchGamingEntities()) 
    { 
     return Json(
      from m in db.MatchTypes 
      where m.GameId == id 
      select new { 
       Text = m.MatchTypeName, 
       Value = m.MatchTypeId 
      }, 
      JsonRequestBehavior.AllowGet 
     ); 
    } 
} 

,然後的JavaScript:

$(function() { 
    $('#GameId').change(function() { 
     var url = '@Url.Action("GetMatchType", "MatchManager")'; 
     var data = { id: $(this).val() }; 
     $.get(url, data, function (games) { 
      var ddlSelectedProduct = $('#MatchTypeId'); 
      ddlSelectedProduct.empty(); 
      $.each(games, function(index, game) { 
       ddlSelectedProduct.append(
        $('<option/>', { 
         value: game.Value, 
         text: game.Text 
        }) 
       ); 
      }); 
     }); 
    }); 
}); 
+0

嘿,我完全複製你的代碼,它不起作用。我編輯了我的問題並添加了我的視圖。 – anthonypliu 2011-03-21 21:18:27

+0

@anthonypliu,什麼沒有工作?你有錯誤嗎?控制器操作是否被擊中?使用FireBug查看服務器對AJAX調用的響應是什麼?也許我在輸入時犯了一些錯誤。 – 2011-03-21 21:19:04

+0

嘿,我檢查了螢火蟲,這是響應:執行當前Web請求期間生成未處理的異常。關於異常的來源和位置的信息可以使用下面的異常堆棧跟蹤來標識。 – anthonypliu 2011-03-22 20:48:35

1

不是試圖自己解析JSON,而是讓jQuery處理它。

只需更換那麼你$.get$.getJSON

jQuery將自動嘗試將響應作爲JSON解碼並可以自動訪問它像一個普通的對象。

jQuery.getJSON上的文檔。

0
$(document).ready(function() { 
    $("#GameId").change(function() { 
     $.getJSON('/MatchManager/GetMatchType/' + $(this).val(), function (Games) { 
      var ddlSelectedProduct = $("#MatchTypeId"); 
      $("#MatchTypeId > option").remove(); 
      for (i = 0; i < Games.length; i++) { 
       ddlSelectedProduct.append($("<option />").val(Games[i].Value).text(Games[i].Text)); 
      } 
     }); 
    }); 
});