Get Name using Ajax and Json and Viewbag

2 posts, 0 answers
  1. Nilrukshi
    Nilrukshi avatar
    1 posts
    Member since:
    Jun 2015

    Posted 15 Jul 2015 Link to this post

    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.​

  2. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 20 Jul 2015 Link to this post

    Hello Nilrukshi,

    Thank you for posting to the Telerik Developer Forums.

    You have submitted your support inquiry for Telerik UI for ASP.NET MVC under the Telerik Platform product forums. I am afraid that Telerik Platform does not include support for the Telerik product you are using and we cannot provide a resolution for your technical question. That is why I'd suggest that you consult the Telerik UI for ASP.NET MVC forums or the referenced there community resources. Also, if your company holds a license for Telerik UI for ASP.NET MVC, please, contact the license holder to submit a support ticket for the appropriate product. 

    In regard to your code, I'd suggest that you examine the following:
     - The controller receives the correct Id as a string
     - The country name is successfully retrieved from the database
     - The con.CountryName value obtained from the ViewBag is not undefined - this value is returned to the client code and prints undefined but the problem might be in the server code.

    Regards,
    Anton Dobrev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top