Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>
Posted
Updated 26-Jun-11 20:37pm
v2

1 solution

Hi Javad,

Your code sample is very hard to read and is not well formatted, but I think I can get the gist of what you're trying to do.

Read this[^] blog post to get a working solution.

If you're looking for other model binding scenarios (going client->server) you could also read up here: ASP.NET MVC and jQuery Part 4 – Advanced Model Binding[^].

Cheers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900