$(document).ready(function () {
var saveResult = '@ViewBag.SaveResult';
if (saveResult.toLowerCase() == 'true') {
var saveResultMessage = '@ViewBag.SaveResultMessage';
if (saveResultMessage == '') saveResultMessage = '儲存成功';
$bui.alertOK(saveResultMessage);
}
else if (saveResult.toLowerCase() == 'false') {
var saveResultMessage = '@ViewBag.SaveResultMessage';
if (saveResultMessage == '') saveResultMessage = '儲存失敗';
$bui.alertError(saveResultMessage);
}
});
這是我的一段Jquery,主要是給下面這種controller使用
public ActionResult AddOrUpdate(HomeVisitViewModel model)
{
if(!ModelState.IsValid)
{
ViewBag.SaveResult = false;
return View("HomeVisitEdit", model);
}
PD_HOME_VISIT pdhomevisit = new PD_HOME_VISIT();
HomeVisitService hvsrvc = new HomeVisitService();
bool saveResult = false;
bool isAdd = false;
//如果GUID空值或null就進行新增動作
if (String.IsNullOrWhiteSpace(model.GUID) || new Guid(model.GUID) == Guid.Empty)
{
isAdd = true;
}
if (model.NUTRITIONAL_SUPPLEMENT == "1")
{
model.NUTRITIONAL_SUPPLEMENT_CODE = string.Join(",", model.NUTRITIONAL_SUPPLEMENT_CODES.ToArray());
}
if (model.HOME_CARE_CODES != null) model.HOME_CARE_CODE = string.Join(",", model.HOME_CARE_CODES.ToArray());
if (model.LIQUID_EXCHANGE_CODES != null) model.LIQUID_EXCHANGE_CODE = string.Join(",", model.LIQUID_EXCHANGE_CODES.ToArray());
if (model.LE_PREPARE_CODES != null) model.LE_PREPARE_CODE = string.Join(",", model.LE_PREPARE_CODES.ToArray());
if (model.LE_LIQUID_EXCHANGE_CODES != null) model.LE_LIQUID_EXCHANGE_CODE = string.Join(",", model.LE_LIQUID_EXCHANGE_CODES.ToArray());
if (model.END_LE_LIQUID_EXCHANGE_CODES != null) model.END_LE_LIQUID_EXCHANGE_CODE = string.Join(",", model.END_LE_LIQUID_EXCHANGE_CODES.ToArray());
if (model.PD_RECORD_CODES != null) model.PD_RECORD_CODE = string.Join(",", model.PD_RECORD_CODES.ToArray());
if (model.CATHETER_OUTLET_CODES != null) model.CATHETER_OUTLET_CODE = string.Join(",", model.CATHETER_OUTLET_CODES.ToArray());
if (model.DS_STOIRAGE_CODES != null) model.DS_STOIRAGE_CODE = string.Join(",", model.DS_STOIRAGE_CODES.ToArray());
if (model.ORDER_COMOLIANCE_CODES != null) model.ORDER_COMOLIANCE_CODE = string.Join(",", model.ORDER_COMOLIANCE_CODES.ToArray());
model.VISITDATE = DateHelper.ToDate(model.VisitDateString);
model.HD_DATE = DateHelper.ToDate(model.HD_DateString);
model.PD_DATE = DateHelper.ToDate(model.PD_DateString);
model.FIRST_PD_PLACE_DATE = DateHelper.ToDate(model.First_PD_Place_Date_String);
model.PERITONITIS_DATE = DateHelper.ToDate(model.Peritionitis_Date_String);
model.EXIT_SITE_INFECTION_DATE = DateHelper.ToDate(model.Exit_Site_Infection_String);
pdhomevisit = model.ToHomeVisit(SessionUserInfo().UserID);
if (isAdd)
{
saveResult = hvsrvc.AddHomeVisit(pdhomevisit);
}
else
{
saveResult = hvsrvc.UpdateHomeVisit(pdhomevisit);
}
ViewBag.SaveResult = saveResult;
HomeVisitViewModel newmhomevisit = GetHomeViewModel(pdhomevisit.GUID.ToString());
newmhomevisit.Assess();
return View("HomeVisitEdit", newmhomevisit);
}
只要儲存成功,就會顯示儲存成功這個訊息,反之失敗也是,現在我想做一個新的訊息
是只要我HomeVisitViewModel裡的這個欄位
public string PRICE { get; set; }
只要新增或編輯時沒有值就會顯示請計價這個訊息,想請問這個controller我該如何寫?
目前我只暫時寫好前端
var priceResult = '@ViewBag.priceResult';
if (saveResult.toLowerCase() == 'false') {
var priceResultMessage = '@ViewBag. priceResultMessage';
if (priceResultMessage == '') priceResultMessage = '請計價';
$bui.alertError(priceResultMessage);
}
只要新增或編輯時沒有值就會顯示請計價這個訊息,想請問這個controller我該如何寫?
[HttpPost]
public IActionResult CreateOrEdit(HomeVisitViewModel model)
{
if (ModelState.IsValid)
{
if (String.IsNullOrEmpty(model.PRICE))
{
ViewBag.priceResult = "false";
ViewBag.priceResultMessage = "請計價";
return View(model);
}
// 進行新增或編輯的邏輯
// ...
ViewBag.priceResult = "true";
return RedirectToAction(nameof(Index));
}
return View(model);
}
範例程式僅供參考