Monibrata Monibrata - 6 months ago 18
jQuery Question

How to pass multiple json object in Ajax posting to get the same in conTroller

I have gone through all possible questions here and in google. Didn't find anything useful or maybe I don't know how to change the code accordingly. New in JQuery. Please see my code below.

In a form, master entry and detail entry is there. Here Employee part is the Master and the Member part is the detail and contains multiple rows. I need to pass all recordS on click of a button.

var Employee = {
"Title": "",
"FirstName": "",
"MiddleName": "",
"LastName": "",
"Gender": ""
}
Employee.Title = $('#ddltitle').val();
Employee.FirstName = $('#txtfname').val();
Employee.MiddleName = $('#txtmname').val();
Employee.LastName = $('#txtlname').val();
Employee.Gender = $('#ddlgender').val();

var Member = {
"Membername": "",
"Relation": "",
"Gender": "",
"DOB": "",
"Age": ""
}

Member.Membername = $('#txtmem1').val() + "," + $('#txtmem2').val() + "," + $('#txtmem3').val() + "," + $('#txtmem4').val() + "," + $('#txtmem5').val();
Member.Relation = $('#ddlrel1').val() + "," + $('#ddlrel2').val() + "," + $('#ddlrel3').val() + "," + $('#ddlrel4').val() + "," + $('#ddlrel5').val();
Member.Gender = $('#ddlgen1').val() + "," + $('#ddlgen2').val() + "," + $('#ddlgen3').val() + "," + $('#ddlgen4').val() + "," + $('#ddlgen5').val();
Member.DOB = $('#txtdob1').val() + "," + $('#txtdob2').val() + "," + $('#txtdob3').val() + "," + $('#txtdob4').val() + "," + $('#txtdob5').val();

if ($("#btnSubmit").val() == "Submit") {
var url = "/Employee/InsertEmployee";
$.post(url,Employee,Member, function (data) {
$.each(data, function (i, response) {
if (response.Result == "Success") {
alert("Employee Inserted Successfully");
var url = window.location.href;
window.location.href = url;
ClearFields();
}
else {
alert(response.Error);
var url = window.location.href
window.location.href = url;
ClearFields();
}
});
});
return;
}


My Controller code as below:

public JsonResult InsertEmployee(DMEmployee objEmp,DMMember objMem)
{
BLLEmployee obl = new BLLEmployee();
int i = obl.InsertEmployee(objEmp, objMem);
int i = 0;
List<ResultMessage> l = new List<ResultMessage>();
ResultMessage Rescls;
if (i > 0)
{
Rescls = new ResultMessage();
Rescls.Result = "Success";
}
else
{
Rescls = new ResultMessage();
Rescls.Error = "Enter Valid Credentials";
}
l.Add(Rescls);
return Json(l, JsonRequestBehavior.AllowGet);
}


DMEmployee and DMMember classes are as below

public class DMEmployee
{
//Employee Details
public string Title { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
}

public class DMMember
{
//Family Details
public string Membername { get; set; }
public string Relation { get; set; }
public string Gender { get; set; }
public string DOB { get; set; }
public int Age { get; set; }
}


Now I need to pass these master and detail values to controller. For detail, I am passing comma separated values to controller. It is not showing any error, break point is also working.

In controller method I am getting objects value as null. Why could that be?

Answer

Try this way

             var Employee = {
            Title: $('#ddltitle').val(),
            FirstName: $('#txtfname').val(),
            MiddleName:$('#txtmname').val(),
            LastName: $('#txtlname').val(),
            Gender: $('#ddlgender').val()
        }

        var Member = {
            Membername: $('#txtmem1').val() + "," + $('#txtmem2').val() + "," + $('#txtmem3').val() + "," + $('#txtmem4').val() + "," + $('#txtmem5').val(),
            Relation: $('#ddlrel1').val() + "," + $('#ddlrel2').val() + "," + $('#ddlrel3').val() + "," + $('#ddlrel4').val() + "," + $('#ddlrel5').val(),
            Gender: $('#ddlgen1').val() + "," + $('#ddlgen2').val() + "," + $('#ddlgen3').val() + "," + $('#ddlgen4').val() + "," + $('#ddlgen5').val(),
            DOB: $('#txtdob1').val() + "," + $('#txtdob2').val() + "," + $('#txtdob3').val() + "," + $('#txtdob4').val() + "," + $('#txtdob5').val(),
            Age: ""
        }

 //creating json multiple object

        var postData = {
            objEmp: Employee,
            objMem: Member
        };
            $.ajax({
                type: "post"
                url: url.
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(postData),
                success: function (data) {
                    //do you actions
                }
            });