0

Hi I have a view in which I wrote a jquery script to pass a html table values into a JSON object as shown below;

            var contactData = [];
            $('#tblContacts > tbody > tr').each(function () {
                contactData.push({
                    firstName: $(this).find('input.c-firstname').val(),
                    lastname: $(this).find('input.c-lastname').val(),
                    phone: $(this).find('input.c-phone').val(),
                    email: $(this).find('input.c-email').val()
                })
            })

Though I left out part of the code above, but I confirmed that contactData object has the required data. So I got no issues there.

My Challenge: Now I need the object contactData passed into my controller method to get the values into the database as shown below;

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(CreateBusinessPartnerVM businessPartnerVM)
        {
            if (ModelState.IsValid)
            {
                var businessPartner = new BusinessPartner
                {
                    CardCode = prefixCardCode + businessPartnerVM.CardName.ElementAt(0) + DateTime.Now.ToString(),
                    CardName = businessPartnerVM.CardName,
                    CardType = businessPartnerVM.CardType,
                    Email = businessPartnerVM.Email,
                    Phone = businessPartnerVM.Phone,
                    IsActive = businessPartnerVM.IsActive,
                    ContactPersons = //contactData from the jquery script in the view
                };
            }
        }

I have searched and still not found something similar with my requirement. I hope to get a fast help here. Thanks.

7
  • Based on your code i guess your are using a form to submit data to your controller. If so you can add the data to a hidden input type.
    – mausinc
    Commented Aug 16, 2017 at 12:09
  • Have you tried using the JQuery AJAX library? Or if you can't use JQuery, Javascript has a built-in XMLHttpRequest class.
    – G.Hunt
    Commented Aug 16, 2017 at 12:15
  • @mausinc That's right. The html table I convert to json object is inside a form tag. Question is how do I pass the object into a hidden input type? Please forgive me if it's a simple task. I just don't know how to do it. Commented Aug 16, 2017 at 12:20
  • 1
    Check this answer stackoverflow.com/questions/39501843/… Check with your chrome inspector the id of the hidden field and add the data like this $('#hiddenFieldId').val(contactData);
    – mausinc
    Commented Aug 16, 2017 at 12:35
  • 1
    great i updated your question with the answer so people looking for the same can find the answer easier.
    – mausinc
    Commented Aug 17, 2017 at 8:52

2 Answers 2

1

Based on your code i guess your are using a form to submit data to your controller. If so you can add the data to a hidden input type.

Check with your chrome inspector the id of the hidden field and add the data like this

$('#hiddenFieldId').val(contactData);
1
  • And I used Newtonsoft's Json to deserialize the string back to json object and used it the way I wanted. Thank you so much for your lead. Commented Aug 18, 2017 at 9:59
1

You need to edit your CreateBusinessPartnerVM model.

Add a new property that is the array of ContactData object. This way you can post contact data in the same model for business partner and process it further.

somewhat like this;

public class CreateBusinessPartnerVM {
    .... other properties...
    public ContactData[] ContactList { get; set; }
}

public class ContactData {
   public string FirstName {get; set;}
   public string LastName {get; set;}
   public string Phone {get; set;}
   public string Email {get; set;}
}

Once this is up, you can pass the js contactData object along the main object that is being submitted.

* New Edit*

I see your table already has input fields. You just need to do 2 things to submit Contact Data

  1. make sure your table is withing the form
  2. name all the input fields in the table as "ContactPersons[0].firstName" and "ContactPersons[0].lastName" The zero will be incremented with each row in the table. i.e. 0 for first row and 1 for second and so on. This will post the object as array of ContactPersons with the main object.

Just make sure you use names of fields right and also their case.

7
  • This is similar to the ContactPersons property in the View Model. I have this already. But how do I pass the json ContactData into the property through the main object? Commented Aug 16, 2017 at 12:31
  • ok I can help you with that. Can you show me how your current main object is being passed?
    – Shahbaz
    Commented Aug 16, 2017 at 12:33
  • Thanks Shabhbaz. Kindly look at the question where I have my controller. The main object is the BusinessPartner which is being created. and the parameter is the ViewModel CreateBusinessPartnerVM. Or would you want to see the ViewModel itself? Commented Aug 16, 2017 at 12:38
  • I want to see the javascript part where you submit your main object.. on the view
    – Shahbaz
    Commented Aug 16, 2017 at 12:43
  • Oh no I don't have that. I intend to do it all on the server side. That's the essence of the controller. Commented Aug 16, 2017 at 12:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.