如何显示列表引用asp.net mvc2中的bye json? [英] how show list cites bye json in asp.net mvc2?

查看:51
本文介绍了如何显示列表引用asp.net mvc2中的bye json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;选项值=&#39;& quot; + country.Value +& quot;&#39;& 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(&quot;~\\Models\\Location.xml&quot;);

var unit = from u in XDocument.Load(strXmlpath).Document.Descendants(&quot;Unit&quot;) select u;
List&lt;SelectListItem&gt; lst = new List&lt;SelectListItem&gt;();
foreach (var u in unit)
{
if (u.Attribute(&quot;ID&quot;).Value == &quot;2&quot;)
{
var city = u.Elements(&quot;city&quot;);
foreach (var c in city)
lst.Add(new SelectListItem { Text = c.Attribute(&quot;Name&quot;).Value, Value = c.Attribute(&quot;ID&quot;).Value });
}

}

ViewData[&quot;cites&quot;] = lst;


return Json(lst);


}






&lt;script type=&quot;text/javascript&quot;&gt;

$(document).ready(function () {


$(function () {
$.getJSON(&quot;/Home/CityList&quot;, function (data) {
var items = &quot;&lt;option&gt;---------------------&lt;/option&gt;&quot;;
$.each(data, function (i, country) {
items += &quot;&lt;option value=&#39;&quot; + country.Value + &quot;&#39;&gt;&quot; + country.Text + &quot;&lt;/option&gt;&quot;;
});
$(&quot;#Countries&quot;).html(items);
});




});
&lt;/script&gt;









&lt;% using (Html.BeginForm()){%&gt;
&lt;label for=&quot;Countries&quot;&gt;Countries&lt;/label&gt;
&lt;select id=&quot;Countries&quot; name=&quot;Countries&quot;&gt;&lt;/select&gt;

&lt;label for=&quot;States&quot;&gt;States&lt;/label&gt;
&lt;select id=&quot;States&quot; name=&quot;States&quot;&gt;&lt;/select&gt;


&lt;% } %&gt;</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(&quot;/Home/CityList&quot;, function (data) {
var items = &quot;&lt;option&gt;---------------------&lt;/option&gt;&quot;;


这篇关于如何显示列表引用asp.net mvc2中的bye json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆