Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, i'm calling a javascript function in an onchange event of a dropdownlist as follows

C#
function poplgas() {
                                $.getJSON('@Url.Action("getLGA", "Ministry")', function (costs) {
                                    var $sel = $("#schLga");
                                    $sel.empty();
                                    $.each(costs, function (index, element) {
                                        $("<option/>").text(element).appendTo($sel);
                                    });
                                });
                            }


this Action in the controller has a model class object as its parameter
C#
public ActionResult getLGA(Category c)
        {
            try
            {
                getMethods.getState(c);
                
                char[] chars = { ',' };

                
                int statepos = c.Name.IndexOf(c.statename); //this gets the position where the state is in the list
                string stateindex = c.ID.ElementAt(statepos).ToString();  //this gets the value of the state ID.

                testStealthServ.countries csd = new testStealthServ.countries();
                List<string> sth = csd.getlocal(stateindex).ToList<string>();
                foreach (var v in sth)
                {
                    string[] splits = v.Split(chars);

                    c.lgaName.Add(splits.ElementAt(0));
                    c.lgaID.Add(splits.ElementAt(1));
                }

                return Json(c.lgaName, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
                //var mess = ex.Message + "\n" + ex.StackTrace;
                //Response.Write("<script>alert(mess);</script>");
            }
        }

my javascript code isn't working because i need to pass An object that contains the parameters for a route. in this case which is the class object Category c;

please how do i do that.
Thanks for your assistance.
Posted

1 solution

Firstly you are using $.getJSON method and it send data through HTTP GET method. Secondly you want to post a complex object from javascript to server which not good(not possible also). It will work if you have simple data like as employeeID not employee object. Hope you get the point.

Next how to overcome your problem:

Step 1: Add model class to model folder
C#
public class Category
    {
        public List<string> ID { get; set; }
        public List<string> Name { get; set; }
        public List<string> lgaID { get; set; }
        public List<string> lgaName { get; set; }
        public int IDTest { get; set; }
    }
}

Step 2: Add action to Controller
C#
[HttpPost]
public ActionResult getLGA(Category catObject)
{
	try
	{
		Category c = new Category();

		char[] chars = { ',' };

		for (int i = 0; i < 10; i++)
		{
			c.lgaName.Add("Manas - " + i.ToString());
		}

		return Json(c.lgaName, JsonRequestBehavior.AllowGet);
	}
	catch (Exception ex)
	{
		throw ex;
	}
}

Step 3: Add code in view page.
HTML
<select id="schllga" class="form-control-select" required></select>

JavaScript
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script>
    var tempObject = ["Manas", "Hero"];
    var jsonObj = { catObject: { ID: null, Name: null, lgaID: null, lgaName: tempObject, IDtest: 29 } };
    
    $.ajax({
        type: "POST",
        url: "/Home/getLGA",
        data: JSON.stringify(jsonObj),
        contentType: "application/json",
        datatype: "json",
        success: function (costs) {
            var $sel = $("#schllga");
            $sel.empty();
            $.each(costs, function (index, element) {
                alert(index);
                $("<option/>").text(element).appendTo($sel);
            });
        }
    });

Note: You may change your logic inside controller action.
 
Share this answer
 
v3

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