Hi All,
I have a Combo Box which can choose a Country Id and once i select the Country Id,I want to Display the Country name in a text box beside the Country Id Combo Box.I am using Ajax and View bag methods to get Country name.I am passing my Country Id on ComboBox on change function and try to retrieve the CountryName in Ajax ,json method.I can get the Country Name in the Controller Action Result(return json) and i couldn't pass my value to Index page which have my Ajax method.I am getting 'undefined' when I alert my data in Ajax method.
My Codes are showing below.
Onchange Function:
function onChange(e) {
$.ajax({
url: 'GetCountryname',
type: 'POST',
dataType: 'json',
data: { countryId: $("#CountryId").val() },
success:
function(data) {
alert(data.con);
$("#CountryName").val(data.con);
}
});
Action Result(Controller)
[HttpPost]
public ActionResult GetCountryname([DataSourceRequest]DataSourceRequest request, string countryId)
{
if (countryId != null)
{
Country county = db.Countries.Find(countryId);
ViewBag.CountryNAME = county.CountryName;
}
var con = new { ViewBag.CountryNAME };
return Json(con, JsonRequestBehavior.AllowGet);
}
Combo Box Codes
@model object
@(Html.Kendo().ComboBoxFor(m => m)
.DataTextField("CountryID")
.DataValueField("CountryID")
.Template("#= CountryID # | #= CountryName #")
.Placeholder("Country ID")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCountryIDForDrop", "FranchisorBrand");
})
.ServerFiltering(true);
})
.Events(events => events.Change("onChange"))
)
Please help me to solve the Issue.