Click here to Skip to main content
16,017,745 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
good day, i have a property set in my model class as shown
C#
public class Category
    {
        public string statename { get; set; }
    }


i got a variable from my view using ajax as shown below,
JavaScript
<script language="javascript">
                            function getlgass() {

                                var staname = $('#mylist option:selected').text();//this is a htmlhelper dropdownlist. i got the selected text from this list and saved it in that variable

                                $.ajax({
                                    url:'/Ministry/sendProp',
                                    type:'POST',
                                    data: JSON.stringify(staname),
                                    contentType: 'application/json; charset=utf-8',
                                    success: function (data) { alert(data.success); },
                                    error: function(){alert("Error")}
                                });// i'm posting it to my controller function using ajax
                                
                                $.getJSON('@Url.Action("getLGA","Ministry")', function (costs) {
                                var $sel = $("#schLga");
                                $sel.empty();
                                $.each(costs, function (index, element) {
                                    $("<option/>").text(element).appendTo($sel);
                                });
                                });
                            }

                            alert("something changed");
                        </script>


i need to pass that variable to the property in my model so that i can use it in the function getLGA to perform a server request
my controller function are as shown
C#
[HttpPost]
        public ActionResult sendProp()
        {
           // i think i'm supposed to attach the value to my model property here, but i don't know how.
            return Json(new { success = true });
        }

        [AllowCrossSiteJson]
        public ActionResult getLGA()
        {
            try
            {
                Category c = new Category();

                getMethods.getState(c);
                
                char[] chars = { ',' };

                int statepos = c.Name.IndexOf(c.statename);//i need that value here
                string stateindex = c.ID.ElementAt(statepos).ToString();

                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;
            }
        }


Thanks for your assistance.
Posted

C#
$.ajax({
                                    type: "POST",
                                    url: "/Ministry/sendProp",
                                    data: { "statename": staname },
                                    success: function (data) { alert(data.success); },
                                    error: function () { alert("Error"); }
                                });


staname is the variable containing the string i want to bind to my model property.
statename is the model property i want to bind with staname

C#
[HttpPost]
        public ActionResult sendProp(Category c)
        {            
            string state = c.statename;
            return this.Json(new { success = state});
        }

then you access it this way in the controller
 
Share this answer
 
Comments
Mohsin_Khan_ 20-Jan-16 0:53am    
This is correct way to post data to web api method. Above code is working ?
Use below code :

Javascript Code
JavaScript
$.ajax({
       url:'/Ministry/sendProp',
       type:'POST',
       data: {"statename" : "MP"},
       contentType: 'application/json',
       success: function (data) { alert(data.success); },
       error: function(){alert("Error")}
   });


Web API Code
C#
[HttpPost]
public ActionResult sendProp(Category model)
{
    return Json(new { StateName= model.statename });
}

Hopefully, it will work ...!
 
Share this answer
 
Comments
EasyHero 19-Jan-16 7:55am    
Not still mapping the value. I don't understand why "MP" is in the data statement of the Ajax query and "StateName" is in the httppost method.
EasyHero 19-Jan-16 8:28am    
I'm retrieving the selected text of a dropdown list into var 'staname' in the onchange event of the dropdown . Still in that on change event, I want the value of var 'staname' which is the text to be assigned to a model property. That's what I'm trying to achieve.

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