Securely post multiple models in one request in MVC application

Securely post multiple models in one request in MVC application

Real-Time Sample: CRM in MVC

Suppose we have a requirement of adding a customer with Their Branch, Follow up details

Step 1: Firstly make jquery object models to be post data in single request

Customer Model
var customerViewModel = {
CustomerId: hdnCustomerId.val(),
CustomerCode: txtCustomerCode.val().trim(),
CustomerName: txtCustomerName.val().trim(),
ContactPersonName: txtContactPersonName.val().trim(),
Designation: txtDesignation.val().trim(),
Email: txtEmail.val().trim(),
MobileNo: txtMobileNo.val().trim(),
ContactNo: txtContactNo.val(),
Fax: txtFax.val().trim(),
PrimaryAddress: txtPrimaryAddress.val().trim(),
City: txtCity.val().trim(),
StateId: ddlState.val(),
CountryId: ddlCountry.val(),
PinCode: txtPinCode.val().trim(),
CSTNo: txtCSTNo.val().trim(),
TINNo: txtTINNo.val().trim(),
PANNo: txtPANNo.val().trim(),
GSTNo: txtGSTNo.val().trim(),
ExciseNo: txtExciseNo.val().trim(),
EmployeeId: hdnEmployeeId.val(),
CustomerTypeId: ddlCustomerType.val(),
CreditLimit: txtCreditLimit.val().trim(),
CreditDays: txtCreditDays.val().trim(),
Customer_Status: chkStatus,
AnnualTurnOver:txtAnnualTurnOver.val(),
GST_Exempt: GSTExempt

};

Customer Branch Model
var customerBranch = {
CustomerBranchId: customerBranchId,
BranchName: branchName,
ContactPersonName: bContactPersonName,
Designation: bDesignation,
Email: bEmail,
MobileNo: bMobileNo,
ContactNo: bContactNo,
Fax: bFax,
PrimaryAddress: bAddress,
City: bCity,
StateId: bStateId,
StateName: bStateName,
CountryId: bCountryId,
PinCode: bPinCode,
CSTNo: bCSTNo,
TINNo: bTINNo,
PANNo: bPANNo,
GSTNo: bGSTNo,
AnnualTurnOver:annualTurnOverBranch

};
customerBranchList.push(customerBranch);

Cstomer Follow Up Model
var customerFollowup = {
CustomerFollowUpId: CustomerFollowUpId,
FollowUpActivityTypeId: FollowUpActivityTypeId,
FollowUpActivityTypeName: FollowUpActivityTypeName,
FollowUpDueDateTime: FollowUpDueDateTime,
FollowUpReminderDateTime: FollowUpReminderDateTime,
FollowUpRemarks: FollowUpRemarks,
Priority: Priority,
PriorityName: PriorityName,
FollowUpStatusId: FollowUpStatusId,
FollowUpStatusName: FollowUpStatusName,
FollowUpStatusReason: FollowUpStatusReason,
FollowUpByUserId: FollowUpByUserId,
FollowUpByUserName: followUpByUserName
};

customerFollowUpList.push(customerFollowup);

Step 2: Make ajax call to be post data

var requestData = {
customerViewModel: customerViewModel,
customerBranchs: customerBranchList,
customerProducts: customerProductList,
customerFollowUps: customerFollowUpList };
$.ajax({
url: “../Customer/AddEditCustomer”,
cache: false,
type: “POST”,
dataType: “json”,
data: JSON.stringify(requestData),
contentType: ‘application/json’,
success: function (data) {
if (data.status == “SUCCESS”) {
ShowModel(“Alert”, data.message);
ClearFields();
setTimeout(
function () {
window.location.href = “../Customer/AddEditCustomer”;
}, 2000);

$(“#btnSave”).show();
$(“#btnUpdate”).hide();
}
else {
ShowModel(“Error”, data.message)
}
},
error: function (err) {
ShowModel(“Error”, err)
}
});

Step 3: Pass data to Action Method to be perform data

public ActionResult AddEditCustomer(CustomerViewModel customerViewModel, List<CustomerBranchViewModel> customerBranchs, List<CustomerProductViewModel> customerProducts,List<CustomerFollowUpViewModel> customerFollowUps)
{ }

If any issues contact with us on : http://www.dotnethub.co.in/contact-us

Leave a Reply

Your email address will not be published. Required fields are marked *