Click here to Skip to main content
15,886,776 members
Articles / JSON
Article

Json call in MVC

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 7.3K   1  
I've working with my ajax call in my MVC project and I've decided to implement json call here is my example: View:  

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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!!
 
This article was originally posted at http://wiki.asp.net/page.aspx/1691/json-call-in-mvc

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --