如何获得降JSON对象了吗? [英] How to get the Json object for drop down?
本文介绍了如何获得降JSON对象了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更试穿后级联下拉我决定jQuery来做到这一点。
after trying much more on cascading drop down I decided to do it by Jquery.
这是我cityController
This is in my cityController
public ActionResult States(int id)
{
AcademicERP.Models.AcademicERPDataContext dc = new AcademicERPDataContext();
var states = from s in dc.States
where s.CountryID == id
select s;
return Json(states.ToList());
}
和我试图从
城市/生成具有脚本页
var ddlCountry;
var ddlStateID;
function pageLoad() {
ddlStateID = $get("StateID");
ddlCountry = $get("CountryID");
$addHandler(ddlCountry, "change", bindOptions);
bindOptions();
}
function bindOptions() {
ddlStateID.options.length = 0;
var CountryID = ddlCountry.value;
if (CountryID) {
// some logic to call $.getJSON()
}
和我有DD的意见
<%= Html.DropDownList("CountryID") %>
<select name="StateID" id="StateID"></select>
所以这将是一个参数的getJSON?
我指的<一个href=\"http://stephenwalther.com/blog/archive/2008/09/07/asp-net-mvc-tip-41-creating-cascading-dropdown-lists-with-ajax.aspx\"相对=nofollow>博客。但不工作。
推荐答案
这样的:
function bindOptions()
{
ddlStateID.options.length = 0;
var CountryID = ddlCountry.value;
if (CountryID)
{
var url = "/<YOUR CONTROLLER NAME>/States/" + CountryID;
$.get(url, function(data) {
// do you code to bind the result back to your drop down
});
}
}
OR,而不是使用页面加载,我就纯粹的jQuery使用它:
OR, instead of using pageLoad, I would use it purely by jQuery:
$(document).ready(function() {
$("#CountryID").change(function() {
var strCountryIDs = "";
$("#CountryID option:selected").each(function() {
strCountryIDs += $(this)[0].value;
});
var url = "/<YOUR CONTROLLER NAME>/States/" + strCountryIDs;
$.getJSON(url, null, function(data) {
$("#StateID").empty();
$.each(data, function(index, optionData) {
$("#StateID").append("<option value='"
+ optionData.StateID
+ "'>" + optionData.StateName
+ "</option>");
});
});
});
});
类似的东西...
Something like that ...
这篇关于如何获得降JSON对象了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文