У меня есть длинная форма, которая публикует данные в 2, 3 или 4 таблицы, в зависимости от того, какие флажки выбраны в представлении.
Мой первый вопрос: я получаю сообщение об ошибке «не все пути кода возвращают значение»
Я понимаю, что получаю эту ошибку, когда есть необработанный случай, но я не могу поймать этот случай. Можете ли вы помочь?
Мой второй вопрос: могу ли я завернуть
var s_request = new S_REQUEST {// сопоставить свойства модели со свойствами vm};
в функции, чтобы вызвать его несколько раз?
Есть ли комментарии о том, как организован код?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Initiate(SViewModel vm){
string selectedWorkStepAction = Request["initiationSteps"].ToString();
switch (selectedWorkStepAction)
{
case "0":
//throw error "Please select an work step action before submission!"
//stop processing. get out of action
//don't submit form
return View(vm);
case "1":
if (vm.Coordinates == true)
{
if (vm.ShareWithContractors == false)
{
using (var context = new SatelliteServicesDBContext())
{
context.Database.Log = Console.Write;
using (DbContextTransaction transaction = context.Database.BeginTransaction())
{
try
{
if (ModelState.IsValid)
{
var s_request = new S_REQUEST
{
//map required properties with coordinates properties, keep few properties null, they will be updated later
//will get written again
//version one
};
db.S_REQUEST.Add(s_request);
db.SaveChanges();
var a_form = new A_FORM
{
//map all properties except one keep it null, will be updated later
};
db.A_FORM.Add(a_form);
db.SaveChanges();
var S_Contractor = new S_CONTRACTOR
{
//reads results from ajax function
};
db.S_CONTRACTOR.Add(S_Contractor);
db.SaveChanges();
//call approve method if data is saved successfully
return View("Success");
}
return View(vm);
}
catch (Exception)
{
transaction.Rollback();
ModelState.AddModelError("SaveError", "Unable to save changes."); //TODO make sure it shows in view not YSOD
//return View(vm);
}
//return View(vm);
}
}
}
else
{
using (var context = new SatelliteServicesDBContext())
{
context.Database.Log = Console.Write;
using (DbContextTransaction transaction = context.Database.BeginTransaction())
{
try
{
if (ModelState.IsValid)
{
var s_request = new S_REQUEST
{
//map required properties with coordinates properties, keep few properties null, they will be updated later
//version one
};
db.S_REQUEST.Add(s_request);
db.SaveChanges();
var a_form = new A_FORM
{
//map all properties except one keep it null, will be updated later
};
db.A_FORM.Add(a_form);
db.SaveChanges();
//call approve method if data is saved successfully
return View("Success");
}
return View(vm);
}
catch (Exception)
{
transaction.Rollback();
ModelState.AddModelError("SaveError", "Unable to save changes."); //TODO make sure it shows in view not YSOD
//return View(vm);
}
}
}
}
}
else //Coordinates checkbox is not checked
{
if (vm.ShareWithContractors == false)
{
using (var context = new SatelliteServicesDBContext())
{
context.Database.Log = Console.Write;
using (DbContextTransaction transaction = context.Database.BeginTransaction())
{
try
{
if (ModelState.IsValid)
{
var s_request = new S_REQUEST
{
//only required properties
//version two
};
db.S_REQUEST.Add(s_request);
db.SaveChanges();
var a_form = new A_FORM
{
//map all properties except one keep it null, will be updated later
};
db.A_FORM.Add(a_form);
db.SaveChanges();
var file = new MAP
{
//reads results from ajax function 1
//map all properties
};
db.MAP.Add(file);
db.SaveChanges();
var S_Contractor = new S_CONTRACTOR
{
//reads results from ajax function 2
//map all properties
};
db.S_CONTRACTOR.Add(S_Contractor);
db.SaveChanges();
//call approve method if data is saved successfully
return View("Success");
}
return View(vm);
}
catch (Exception)
{
transaction.Rollback();
ModelState.AddModelError("SaveError", "Unable to save changes."); //TODO make sure it shows in view not YSOD
//return View(vm);
}
}
}
}
else
{
using (var context = new SatelliteServicesDBContext())
{
context.Database.Log = Console.Write;
using (DbContextTransaction transaction = context.Database.BeginTransaction())
{
try
{
if (ModelState.IsValid)
{
var s_request = new S_REQUEST
{
//only required properties
//version two
};
db.S_REQUEST.Add(s_request);
db.SaveChanges();
var a_form = new A_FORM
{
//map all properties except one keep it null, will be updated later
};
db.A_FORM.Add(a_form);
db.SaveChanges();
var file = new MAP
{
//reads results from ajax function
};
db.MAP.Add(file);
db.SaveChanges();
//call approve method if data is saved successfully
return View("Success");
}
return View(vm);
}
catch (Exception)
{
transaction.Rollback();
ModelState.AddModelError("SaveError", "Unable to save changes."); //TODO make sure it shows in view not YSOD
//return View(vm);
}
}
}
}
}
break;
case "2":
try
{
if (ModelState.IsValid)
{
var s_request = new S_REQUEST
{
//only required properties
//version two
};
db.S_REQUEST.Add(s_request);
db.SaveChanges();
//delegate method should be called if data is saved successfully
return View("Success");
}
return View(vm);
}
catch
{
ModelState.AddModelError("SaveError", "Unable to save changes.");
//TODO make sure it shows in view not YSOD
//Console.Write("check error: " + ex);
//return View(vm); //should be inside catch, right?
}
break;
case "3":
//don't save to any table
//call cancel method
return RedirectToAction("Services", "Home");
}
}