如何显示列表引用asp.net mvc2中的bye json? [英] how show list cites bye json in asp.net mvc2?
问题描述
hi
看到此代码
< pre lang ="xml">公用JsonResult CityList()
{
字符串strXmlpath = Server.MapPath(&"~~ \\ Models \\ Location.xml& quot;);
var unit =从XDocument.Load(strXmlpath).Document.Descendants(&"Unit& quot;))中的u选择u;
List& lt; SelectListItem& gt; lst =新列表& lt; SelectListItem& gt;();
foreach(单位为var u)
{
if(u.Attribute(&"ID& quot;).Value ==& quot; 2& quot;)
{
var city = u.Elements(& quot; city& quot));
foreach(城市中的var c)
lst.Add(新的SelectListItem {文本= c.Attribute(& quot;名称& quot;).Value,值= c.Attribute(& quot; ID& quot;).Value});
}
}
ViewData [&"cites&"] = lst;
返回Json(lst);
}
& lt; script type =& quot;文本/javascript& quot;& gt;
$(document).ready(function(){
$(function(){
$ .getJSON(&"/Home/CityList&",函数(数据){
var items =& quot;< option& gt; ---------------------& lt;/option& gt;" ;;
$ .each(data,function(i,country){
项目+ =& quot;选项值='& quot; + country.Value +& quot;'& gt;" + country.Text +&& lt;/option& gt;& quot ;;
});
$(&#Countries&").html(items);
});
});
& lt//script& gt;
& lt;%使用(Html.BeginForm()){%& gt;
< label for =&"Countries& gt; Countries& lt;/label& gt;
& lt;选择id ="国家"名称=&国家/地区"</select& gt;
& lt; label for =&"States& gt; States& lt;/label& gt;
& lt;选择id =&状态&"名称=&状态& gt;</select& gt;
& lt;%}%& gt;/pre>
但不在< selec></select>
hi
see this code
<pre lang="xml">public JsonResult CityList()
{
string strXmlpath = Server.MapPath("~\\Models\\Location.xml");
var unit = from u in XDocument.Load(strXmlpath).Document.Descendants("Unit") select u;
List<SelectListItem> lst = new List<SelectListItem>();
foreach (var u in unit)
{
if (u.Attribute("ID").Value == "2")
{
var city = u.Elements("city");
foreach (var c in city)
lst.Add(new SelectListItem { Text = c.Attribute("Name").Value, Value = c.Attribute("ID").Value });
}
}
ViewData["cites"] = lst;
return Json(lst);
}
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$.getJSON("/Home/CityList", function (data) {
var items = "<option>---------------------</option>";
$.each(data, function (i, country) {
items += "<option value='" + country.Value + "'>" + country.Text + "</option>";
});
$("#Countries").html(items);
});
});
</script>
<% using (Html.BeginForm()){%>
<label for="Countries">Countries</label>
<select id="Countries" name="Countries"></select>
<label for="States">States</label>
<select id="States" name="States"></select>
<% } %></pre>
but not show list cites in <selec></select>
推荐答案
(document).ready(function(){
(document).ready(function () {
(function(){
(function () {
.getJSON(&"/Home/CityList&",函数(数据){
var items =& quot;< option& gt; ---------------------& lt;/option& gt;" ;;
.getJSON("/Home/CityList", function (data) {
var items = "<option>---------------------</option>";
这篇关于如何显示列表引用asp.net mvc2中的bye json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!