0

I have a form that has two sections. 3 input fields and another section with 10 checkboxes.

        public class Customerproductdto
        {
            public string CustomerNumber { get; set; }
            public string CustomerName { get; set; }
            public string CustomerPhone { get; set; }       
            List<ProductDetails> GetAllChecked {get;set;}
        }

        public class ProductDetails
        {
            public string ProductName{ get; set; }
        }

Here is jquery code I am using to get all the values of the checkboxes that were checked on my form. They are about 10 and users could check everything.

    var yourArray[]

    $("input:checkbox[name=type]:checked").each(function(){
        yourArray.push($(this).val());
    });

Here is javascript that I use to collect the data and pass to my controller. How can I pass in my array here all in one shot?

    var objdata =
            ({
                CustomerNumber: txtcustnumber,
                CustomerName: txtcustname,
                CustomerPhone: txtphone

                //How do I pass the yourArray here?     

            });

            var url = "@Url.Action("WriteToDb", "Home")";
            var completeData = JSON.stringify({ 'Information': objdata });

            $.get(url, { 'objdata': completeData }, function (data) {
                $('#mainListContent').html(data);
            });

Please note that I will like to deserialize this once I get to the controller. Here is the method.

     public ActionResult WriteToDb(string objdata)
            {
                Customerproductdto getAllTaskSaved = null;

                try
                {
                    var stripOffObjectName = JObject.Parse(objdata)["Information"];
                    var cleanedData = JsonConvert.DeserializeObject<Customerproductdto>(stripOffObjectName.ToString());
                    getAllTaskSaved = _dtcDataService.WriteTaskToDb(cleanedData, "Add");
                }
                catch (Exception ex)
                {
                    logger.Error(ex);

                }

                return PartialView("_Taskdisplay", getAllTaskSaved);

            }
Baba
  • 2,059
  • 8
  • 48
  • 81
  • I would just create a json string and pass that and then do the same thing in the controller you are doing with the base object. – Zach M. Dec 22 '16 at 16:02
  • Do you have some code samples? I am new to all these sorry. – Baba Dec 22 '16 at 16:06
  • http://stackoverflow.com/questions/2295496/convert-array-to-json and http://stackoverflow.com/questions/9586585/convert-json-to-a-c-sharp-array – Zach M. Dec 22 '16 at 16:10
  • I am already doing this. My question is how do I include the array in the var objdata I am sending. – Baba Dec 22 '16 at 16:13
  • `CustomerJsonString: JSON.stringify(yourArray)` where `CustomerJsonString` is a new parameter you add to your object. – Zach M. Dec 22 '16 at 16:17

0 Answers0