Home / ASP.NET Wiki / Javascript / AJAX / JSON / Json call in MVC

Json call in MVC

 Rate It (3)

I've working with my ajax call in my MVC project and I've decided to implement json call here is my example:

 View:

<select id="cmbContinent">

   <option value="---" selected="selected">Choose Continent...</option>

   <option value="eur">Europe</option>

   <option value="ame">America</option>

   <option value="asi">Asia</option>

   <option value="afr">Africa</option>

</select>

<select id="cmbCountries"></select>

 
 
<script language="javascript" type="text/javascript">
$(document).ready(function() {
//****Ajax Example****
   $("#cmbContinente").change(function() {
       fillCombo("cmbCountries", $("#cmbContinent").val());
   });
});
 
//****Ajax Example****
function fillCombo(updateId, value) {
$.getJSON("<%= Url.Action("CountriesPerContinent") %>" + "/" + value,
                 function(data) {
                 $("#"+updateId).empty();
                 $.each(data, function(i, item) {                  $("#"+updateId).append("<option id='" + item.IDCountries +"'>" + item.Name + "</option>");
                   });
          });
}
 
}
</script>
 Controller:
[ActionName("CountriesPerContinent")]public ActionResult GetCountriesPerContinent(string id)
{
var Count = new CountriesModel().GetCountriesPerContinent(id);
return new JsonResult() { Data = Count };
}
 
Model:
public class Country
{
public string Continente { get; set; }
public string Nombre { get; set; }
public string IDPaises { get; set; }
}
public class CountriesModel
{
private List<Pais> paises;public PaisesModel()
{
this.paises = new List<Country>();
this.paises.Add(new Country()
{
Continente = "eur", IDPaises = "es",
Nombre = "Espaa"
});
// return
}
public IEnumerable<Country> GetCountriesPerContinent(string continent)
{
return this.Count.FindAll(x => x.Continent == continent);
}
}
 
 
Hope That Help guys!!
 

Revision number 3, Friday, June 24, 2011 4:17:10 PM by evanorue

Comments

Related Articles

Security Guidelines and Recommendations

There's a great deal of good prescriptive security guidance out there in the form of whitepapers and books. Whitepapers patterns & practices Security Guidance for .NET Framework 2.0 patterns & practices ASP.NET 2.0 Security Guidance patterns &

javascript replace() function replace only single characters

javascript replace() function replace only single characters I have been trying to use javascript replace() to search and replace special individual characters in a string. But having some difficulties. We can use the following way to solve the following problem

F# Samples

Hi in this post only I want to help some links that will help you with F#. I think that F# is a good tool for different knid of things, so if you are working with that this can help you: http://fsharpsamples.codeplex.com/ http://fsharpnews.blogspot.com/2010

Enterprise Library

Enterprise Library "The patterns & practices Enterprise Library from Microsoft is a library of application blocks designed to assist developers with common enterprise development challenges. Application blocks are a type of guidance, provided as source

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)