There are three forms, each labeled as tk1, tk2, tk3, and I have an issue with the input form. When I fill them sequentially, starting from tk1, then tk2, and finally tk3, there is no problem. However, if I fill tk1 and then directly fill tk3 without filling tk2, the data sent to the database becomes null, whereas my expectation is that it should be safe even if tk2 is not filled.
Controller
[HttpPost]
public ActionResult HasilPengadilan(FormCollection form)
{
//updated
if (Accesses.NeedLogin() == "")
{
return RedirectToAction("Index", "Home");
}
if (!Accesses.isCreate(idMenu))
{
return RedirectToAction("Forbidden", "System");
}
int UserId = Convert.ToInt32(Session["UserId"]);
var submitType = form["submit_type"];
if (submitType == "tk1")
{
var selectedValues = form.GetValues("CourtDecisionCaseId_TK1");
var selectedIds = new List<string>();
foreach (var value in selectedValues)
{
selectedIds.Add("'" + value + "'");
}
// Membangun query SQL dengan menggunakan parameterized queries dan IN statement
var query = "SELECT * FROM [Transaction].[Defendants] WHERE DefendantCaseId IN (" + string.Join(",", selectedIds) + ")";
var defendant = db.Defendants.SqlQuery(query).FirstOrDefault();
var fname = "CourtDecisionUUID," +
"CourtDecisionNumber," +
"CourtDecisionDate," +
"CourtDecisionProvinces," +
"CourtDecisionCities," +
"CourtDecisionName," +
"CourtDecisionResume," +
"CourtDecisionVerdict," +
"CourtDecisionDetailVerdict," +
"CourtDecisionStatus," +
"CourtDecisionCaseId";
var fvalue = "NEWID()," +
"'" + form["CourtDecisionNumber_TK1"] + "', " +
"'" + form["CourtDecisionDate_TK1"] + "', " +
"'" + form["CourtDecisionProvinces_TK1"] + "', " +
"'" + form["CourtDecisionCity_TK1"] + "', " +
"'" + form["CourtDecisionName_TK1"] + "', " +
"'" + form["CourtDecisionResume_TK1"] + "', " +
"'" + form["CourtDecisionVerdict_TK1"] + "', " +
"'" + form["CourtDecisionDetailVerdict_TK1"] + "', " +
"1," +
"'" + form["CaseId_TK1"] + "' ";
int lastid = db.Database.SqlQuery<int>("INSERT INTO [Transaction].[CourtDecisions] (" + fname + ") VALUES (" + fvalue.Replace("''", "NULL") + "); SELECT CAST(SCOPE_IDENTITY() AS INT) AS LastId;").SingleOrDefault();
if (lastid == 0)
{
TempData["Notifikasi"] = 2;
TempData["NotifikasiText"] = "Data Gagal Disimpan";
}
else
{
var decision = db.CourtDecisions.SqlQuery("select * from [Transaction].[CourtDecisions] where CourtDecisionId = " + lastid).FirstOrDefault();
var fdefendant = "CourtDecisionDefendantUUID," +
"CourtDecisionDefendantDefendantUUID," +
"CourtDecisionDefendantDefendantNIK," +
"CourtDecisionDefendantStatus," +
"CourtDecisionUUID," +
"CourtDecisionDefendantCreateBy," +
"CourtDecisionDefendantCreateDate";
var fdefendantvalue = "NEWID()," +
"'" + defendant.DefendantUUID + "', " +
"'" + defendant.DefendantNIK + "', " +
"'1'," +
"'" + decision.CourtDecisionUUID + "', " +
"'" + UserId + "', " +
"(GETDATE())";
int lastiddecision = db.Database.SqlQuery<int>("INSERT INTO [Transaction].[CourtDecisionDefendants] (" + fdefendant + ") VALUES (" + fdefendantvalue.Replace("''", "NULL") + "); SELECT CAST(SCOPE_IDENTITY() AS INT) AS LastId;").SingleOrDefault();
if (lastiddecision > 0)
{
Helpers.Global.InserCaseStatus(form["CaseId_TK1"].ToString(), "PEN17", lastid.ToString(), "Peyidikan/MonitoringPelimpahan/Detail/" + lastid.ToString());
TempData["Notifikasi"] = 1;
TempData["NotifikasiText"] = "Data Berhasil Disimpan";
ViewBag.tk2 = 1;
submitType = "tk3";
}
else
{
TempData["Notifikasi"] = 2;
TempData["NotifikasiText"] = "Data Gagal Disimpan";
}
return RedirectToAction("Index");
}
}
else if (submitType == "tk2")
{
var selectedValues = form.GetValues("CourtDecisionCaseId_Banding");
var selectedIds = new List<string>();
foreach (var value in selectedValues)
{
selectedIds.Add("'" + value + "'");
}
// Membangun query SQL dengan menggunakan parameterized queries dan IN statement
var query = "SELECT * FROM [Transaction].[Defendants] WHERE DefendantCaseId IN (" + string.Join(",", selectedIds) + ")";
var defendant = db.Defendants.SqlQuery(query).FirstOrDefault();
var fname = "CourtDecisionUUID," +
"CourtDecisionNumber," +
"CourtDecisionDate," +
"CourtDecisionProvinces," +
"CourtDecisionCities," +
"CourtDecisionName," +
"CourtDecisionResume," +
"CourtDecisionVerdict," +
"CourtDecisionDetailVerdict," +
"CourtDecisionStatus," +
"CourtDecisionCaseId";
var fvalue = "NEWID()," +
"'" + form["CourtDecisionNumber_Banding"] + "', " +
"'" + form["CourtDecisionDate_Banding"] + "', " +
"'" + form["CourtDecisionProvinces_Banding"] + "', " +
"'" + form["CourtDecisionCity_Banding"] + "', " +
"'" + form["CourtDecisionName_Banding"] + "', " +
"'" + form["CourtDecisionResume_Banding"] + "', " +
"'" + form["CourtDecisionVerdict_Banding"] + "', " +
"'" + form["CourtDecisionDetailVerdict_Banding"] + "', " +
"2," +
"'" + form["CaseId_Banding"] + "' ";
int lastid = db.Database.SqlQuery<int>("INSERT INTO [Transaction].[CourtDecisions] (" + fname + ") VALUES (" + fvalue.Replace("''", "NULL") + "); SELECT CAST(SCOPE_IDENTITY() AS INT) AS LastId;").SingleOrDefault();
if (lastid == 0)
{
TempData["Notifikasi"] = 2;
TempData["NotifikasiText"] = "Data Gagal Disimpan";
}
else
{
var decision = db.CourtDecisions.SqlQuery("select * from [Transaction].[CourtDecisions] where CourtDecisionId = " + lastid).FirstOrDefault();
var fdefendant = "CourtDecisionDefendantUUID," +
"CourtDecisionDefendantDefendantUUID," +
"CourtDecisionDefendantDefendantNIK," +
"CourtDecisionDefendantStatus," +
"CourtDecisionUUID," +
"CourtDecisionDefendantCreateBy," +
"CourtDecisionDefendantCreateDate";
var fdefendantvalue = "NEWID()," +
"'" + defendant.DefendantUUID + "', " +
"'" + defendant.DefendantNIK + "', " +
"'2'," +
"'" + decision.CourtDecisionUUID + "', " +
"'" + UserId + "', " +
"(GETDATE())";
int lastiddecision = db.Database.SqlQuery<int>("INSERT INTO [Transaction].[CourtDecisionDefendants] (" + fdefendant + ") VALUES (" + fdefendantvalue.Replace("''", "NULL") + "); SELECT CAST(SCOPE_IDENTITY() AS INT) AS LastId;").SingleOrDefault();
if (lastiddecision > 0)
{
Helpers.Global.InserCaseStatus(form["CaseId_Banding"].ToString(), "PEN17", lastid.ToString(), "Peyidikan/MonitoringPelimpahan/Detail/" + lastid.ToString());
TempData["Notifikasi"] = 1;
TempData["NotifikasiText"] = "Data Berhasil Disimpan";
}
else
{
TempData["Notifikasi"] = 2;
TempData["NotifikasiText"] = "Data Gagal Disimpan";
}
}
return RedirectToAction("Index");
}
else if (submitType == "tk3")
{
var selectedValues = form.GetValues("CourtDecisionCaseId_Kasasi");
var selectedIds = new List<string>();
foreach (var value in selectedValues)
{
selectedIds.Add("'" + value + "'");
}
// Membangun query SQL dengan menggunakan parameterized queries dan IN statement
var query = "SELECT * FROM [Transaction].[Defendants] WHERE DefendantCaseId IN (" + string.Join(",", selectedIds) + ")";
var defendant = db.Defendants.SqlQuery(query).FirstOrDefault();
var fname = "CourtDecisionUUID," +
"CourtDecisionNumber," +
"CourtDecisionDate," +
"CourtDecisionProvinces," +
"CourtDecisionCities," +
"CourtDecisionName," +
"CourtDecisionResume," +
"CourtDecisionVerdict," +
"CourtDecisionDetailVerdict," +
"CourtDecisionStatus," +
"CourtDecisionCaseId";
var fvalue = "NEWID()," +
"'" + form["CourtDecisionNumber_Kasasi"] + "', " +
"'" + form["CourtDecisionDate_Kasasi"] + "', " +
"'" + form["CourtDecisionProvinces_Kasasi"] + "', " +
"'" + form["CourtDecisionCity_Kasasi"] + "', " +
"'" + form["CourtDecisionName_Kasasi"] + "', " +
"'" + form["CourtDecisionResume_Kasasi"] + "', " +
"'" + form["CourtDecisionVerdict_Kasasi"] + "', " +
"'" + form["CourtDecisionDetailVerdict_Kasasi"] + "', " +
"3," +
"'" + form["CaseId_Kasasi"] + "' ";
int lastid = db.Database.SqlQuery<int>("INSERT INTO [Transaction].[CourtDecisions] (" + fname + ") VALUES (" + fvalue.Replace("''", "NULL") + "); SELECT CAST(SCOPE_IDENTITY() AS INT) AS LastId;").SingleOrDefault();
if (lastid == 0)
{
TempData["Notifikasi"] = 2;
TempData["NotifikasiText"] = "Data Gagal Disimpan";
}
else
{
var decision = db.CourtDecisions.SqlQuery("select * from [Transaction].[CourtDecisions] where CourtDecisionId = " + lastid).FirstOrDefault();
var fdefendant = "CourtDecisionDefendantUUID," +
"CourtDecisionDefendantDefendantUUID," +
"CourtDecisionDefendantDefendantNIK," +
"CourtDecisionDefendantStatus," +
"CourtDecisionUUID," +
"CourtDecisionDefendantCreateBy," +
"CourtDecisionDefendantCreateDate";
var fdefendantvalue = "NEWID()," +
"'" + defendant.DefendantUUID + "', " +
"'" + defendant.DefendantNIK + "', " +
"'3'," +
"'" + decision.CourtDecisionUUID + "', " +
"'" + UserId + "', " +
"(GETDATE())";
int lastiddecision = db.Database.SqlQuery<int>("INSERT INTO [Transaction].[CourtDecisionDefendants] (" + fdefendant + ") VALUES (" + fdefendantvalue.Replace("''", "NULL") + "); SELECT CAST(SCOPE_IDENTITY() AS INT) AS LastId;").SingleOrDefault();
if (lastiddecision > 0)
{
Helpers.Global.InserCaseStatus(form["CaseId_Kasasi"].ToString(), "PEN17", lastid.ToString(), "Peyidikan/MonitoringPelimpahan/Detail/" + lastid.ToString());
TempData["Notifikasi"] = 1;
TempData["NotifikasiText"] = "Data Berhasil Disimpan";
}
else
{
TempData["Notifikasi"] = 2;
TempData["NotifikasiText"] = "Data Gagal Disimpan";
}
return RedirectToAction("Index");
}
}
else
{
TempData["Notifikasi"] = 2;
TempData["NotifikasiText"] = "Data Gagal Disimpan";
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
JavaScript from Views
if (@tk2 == 1) {
submit_type = "tk3";
} else if (@tk1 == 1) {
submit_type = "tk2";
} else {
submit_type = "tk1";
}
$("#submit_type").val(submit_type);