iT邦幫忙

0

Viewbag問題請教

  • 分享至 

  • xImage

是這樣的,我做了一個表單,按下儲存後會出現儲存成功的提示

如下圖

https://ithelp.ithome.com.tw/upload/images/20190212/201101323Hh9oQrAdf.png

今日我想做一個新的提示,如果沒金額就會顯示"請計價"這個提示

但我目前失敗,想請問我該如何處理?

這是我新增相關的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);
        }

儲存相關的前端部分

    $(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);
        }                
    });
暐翰 iT邦大師 1 級 ‧ 2019-02-12 21:24:50 檢舉
後端有報錯? 還是瀏覽器打開console有錯誤訊息?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2023-01-30 08:09:39

今日我想做一個新的提示,如果沒金額就會顯示"請計價"這個提示
但我目前失敗,想請問我該如何處理?

Controller部分

[HttpPost]
public ActionResult SaveFormData(FormData formData)
{
    if (string.IsNullOrEmpty(formData.Amount))
    {
        ViewBag.SaveResult = "False";
        ViewBag.SaveResultMessage = "請計價";
    }
    else
    {
        // 其他儲存資料的程式碼
        ViewBag.SaveResult = "True";
        ViewBag.SaveResultMessage = "儲存成功";
    }
    return View();
}

前端部分

$(document).ready(function () {
    var saveResult = '@ViewBag.SaveResult';
    var saveResultMessage = '@ViewBag.SaveResultMessage';
    if (saveResult.toLowerCase() == 'true') {
        if (saveResultMessage == '') {
            saveResultMessage = '儲存成功';
        }
        $bui.alertOK(saveResultMessage);
    }
    else if (saveResult.toLowerCase() == 'false') {
        if (saveResultMessage == '') {
            saveResultMessage = '儲存失敗';
        }
        $bui.alertError(saveResultMessage);
    } else {
        $bui.alertError("請輸入金額");
    }
});

此範例程式僅供參考,假設您使用jQuery框架,並且有一個命名為$bui的提示插件,你可以根據自身情況斟酌修改。

我要發表回答

立即登入回答