2011-03-21 121 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)); 
      } 
     }); 
    }); 
});