Save MVC C# and JS
save C# and JS
================
<script>
$(document).ready(function () {
});
function addToken() {
var url = '/Firebase/AddToken';
var data = {
token: getToken
};
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: data,
success: function (respon) {
console.log(respon);
},
error: function () {
alert("Error Add Token Function");
}
});
}
</script>
=================
function addToken() {
var url = '/Firebase/AddToken';
var data = {
token: "getToken"
};
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: data,
success: function (respon) {
console.log(respon);
},
error: function () {
alert("Error Add Token Function");
}
});
}
========================
function addMailBox() {
var url = '/Rest/AddMailBox';
var data = {
Title: $("#title").val(),
Url: $("#url").val(),
UrlText: $("#urlText").val(),
Type: $("#type").val(),
Body: $("#description").val()
}
$.ajax({
contentType: 'application/json;charset=utf-8',
url: url,
type: 'post',
dataType: 'Json',
data : JSON.stringify(data),
success: function (respon) {
alert(respon);
},
error: function () { alert("Error Function AddMailBox Can't Proccess The Data")}
});
}
public ActionResult AddMailBox(MailBox obj)
{
var defaultType = 1;
if (defaultType != obj.Type) defaultType = obj.Type;
obj.UserId = 0;
obj.CreatedDate = DateTime.Now;
obj.IsRead = false;
obj.IsActive = true;
obj.Type = defaultType;
var data = Repositories.AddMailBox(obj);
var getData = new { data };
return Content(JsonConvert.SerializeObject(getData),"application/json");
}
public static MailBox AddMailBox(MailBox obj) {
using (var dc = new ShowtimeEntities()) {
dc.MailBoxes.Add(obj);
dc.SaveChanges();
return obj;
}
}
======
Save Data List C# MVC
function addMailBox() {
$("#_MailBox").prop("disabled", true);
var url = '/Rest/AddMailBox';
var data = {
Title: $("#title").val(),
Url: $("#url").val(),
UrlText: $("#urlText").val(),
Type: $("#type").val(),
Body: $("#description").val()
}
$.ajax({
contentType: 'application/json;charset=utf-8',
url: url,
type: 'post',
dataType: 'Json',
data : JSON.stringify(data),
success: function (respon) {
btnCancel();
$("#_MailBox").prop("disabled", false);
},
error: function () { alert("Error Function AddMailBox Can't Proccess The Data")}
});
}
public ActionResult AddMailBox(MailBox obj)
{
/// Get Id From User -> This Code For Real Coz Will Get All Id From User
//var getUserId = Repositories.getUserId();
/// Get Id From User -> use this code for data testing
var getUserId = new List<int>() { 1352, 4376, 5571 };
/// Create List Object MailBox
List<MailBox> listMailBox = new List<MailBox>();
/// Loping Data User
for (var i = 0; i < getUserId.Count(); i++)
{
/// Create New Object MailBox
var objMailBox = new MailBox();
/// New Object Filled Base On Obj Except UserId From User Table
objMailBox.UserId = getUserId[i];
objMailBox.CreatedDate = DateTime.Now;
objMailBox.IsRead = false;
objMailBox.IsActive = true;
objMailBox.Type = obj.Type;
objMailBox.Title = obj.Title;
objMailBox.Body = obj.Body;
objMailBox.Url = obj.Url;
objMailBox.UrlText = obj.UrlText;
/// New Object Added To List
listMailBox.Add(objMailBox);
}
/// loop complete
/// Data List Send To Repo
/// Create New Function For List
/// Proceess Insert Data To Mail Box Without Data Returning
Repositories.AddMailBoxList(listMailBox);
var result = new { message = "Success" };
return Content(JsonConvert.SerializeObject(result),"application/json");
}
public static void AddMailBoxList(List<MailBox> obj)
{
using (var dc = new ShowtimeEntities()) {
dc.MailBoxes.AddRange(obj);
dc.SaveChanges();
}
}
Tidak ada komentar