iT邦幫忙

1

片語編輯功能儲存無效果

  • 分享至 

  • xImage

是這樣的,今天我需要做一個片語功能,查詢新增刪除都可以,只有編輯有些問題
我進入編輯模式時沒有抓到我先前下拉市所選的值,存檔後也沒存到我所選的選項
而且我進入偵測模式時都有讀取到我選的值,但儲存後卻沒變化,請問該如何處理?

https://ithelp.ithome.com.tw/upload/images/20181220/2011013207aF4CHaHS.jpg

<div class="box-body">
   <div class="form-flex-container"> 
      <div class="row-flex">
         <div class="col-label">
             <label class="control-label">
                 作業別                                 
             </label>
          </div>
      <div class="col-input">      @Html.DropDownListFor(m=>m.OPERATING,newSelectList(Model.OperatingOptions, "NC_CODE", "NC_NAME"), new { @class = "form-control" })
     </div>
</div><!-- /.row -->

view model

 public class HDPharseViewModel
    {
        public string GUID { get; set; }
        public string CID { get; set; }
        public string OPERATING { get; set; }
        public List<NCODE> OperatingOptions { get; set; }
        public string HOSOUT { get; set; }
        public string DIVISION { get; set; }
        public string USERNAME { get; set; }
        public string PHARSE_CONTENT { get; set; }
        public string STATUS { get; set; }
        public Nullable<System.DateTime> CRE_DATE { get; set; }
        public string CRE_UID { get; set; }
        public Nullable<System.DateTime> MOD_DATE { get; set; }
        public string MOD_UID { get; set; }
        
        public HD_PHARSE ToPharse(string modifyid)
        {
            HD_PHARSE hd = new HD_PHARSE();
            hd.GUID = GUID;
            hd.CID = CID;
            hd.OPERATING = OPERATING;
            hd.HOSOUT = HOSOUT;
            hd.DIVISION = DIVISION;
            hd.USERNAME = USERNAME;
            hd.PHARSE_CONTENT = PHARSE_CONTENT;
            hd.STATUS = STATUS;
            hd.CRE_DATE = DateTime.Now;
            hd.CRE_UID = CRE_UID;
            hd.MOD_DATE = DateTime.Now;
            hd.MOD_UID = MOD_UID;
            return hd;
        }

controller

//編輯
 public ActionResult HDPharseEdit(string id)
        {
            HDPharseViewModel model = GetHDPharseViewModel(id);
            HDPharseService hsrvc = new HDPharseService();
            model.OperatingOptions = GetOperatingOptions();
            return View("HDPharseEdit", model);
        }
        
   //GetHDPharseViewModel方法     
 private HDPharseViewModel GetHDPharseViewModel(string Guid)
        {
            HDPharseService hsrvc = new HDPharseService();
            HD_PHARSE hPharse = hsrvc.FindGuid(Guid);
            HDPharseViewModel mpharse = new HDPharseViewModel();
            mpharse.FromPharse(hPharse);
            return mpharse;
        }
        
        

 //判斷新增或編輯
  public ActionResult AddOrUpdate(HDPharseViewModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.SaveResult = false;
                return View("HDPharseEdit", model);
            }

            HD_PHARSE hpharse = new HD_PHARSE();
            HDPharseService hsrvc = new HDPharseService();
            model.OperatingOptions = GetOperatingOptions();
            bool saveResult = false;
            bool isAdd = false;
            if (String.IsNullOrWhiteSpace(model.GUID) || new Guid(model.GUID) == Guid.Empty)
            {
                isAdd = true;
                model.GUID = Guid.NewGuid().ToString();
            }
            hpharse = model.ToPharse(GetUserGuid());
            if (isAdd)
            {
                saveResult = hsrvc.AddPharse(hpharse);
            }
            else
            {
                saveResult = hsrvc.UpdatePharse(hpharse);
            }
            ViewBag.SaveResult = saveResult;
            return View("HDPharseEdit", model);

GetOperatingOptions 方法

 public List<NCODE> GetOperatingOptions()
        {
            List<NCODE> optins = new List<NCODE>();
            optins.Add(new NCODE() { NC_CODE = "", NC_NAME = "請選擇" });
            optins.Add(new NCODE() { NC_CODE = "0", NC_NAME = "共通作業" });
            optins.Add(new NCODE() { NC_CODE = "1", NC_NAME = "醫囑作業" });
            return optins;
        }

service層

  public bool UpdatePharse (HD_PHARSE Pharse)
        {
            if (Pharse == null || Pharse.GUID == null)
            {
                return false;
            }

            HD_PHARSE dbPharse = _repo.HD_PHARSE.Where(x => x.GUID == Pharse.GUID).FirstOrDefault<HD_PHARSE>();
            if(dbPharse != null)
            {
                dbPharse.OPERATING = Pharse.OPERATING;
                dbPharse.STATUS = Pharse.STATUS;
                dbPharse.PHARSE_CONTENT = Pharse.PHARSE_CONTENT;
                return true;
            }
            else
            {
                return false;
            }
        }

有點長很不好意思,還請各位告訴我該如何解決,謝謝

姓名先碼掉
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2023-01-29 04:58:59

看起來你在控制器中的 HDPharseEdit 方法裡面沒有傳遞所選擇的選項給視圖,所以在顯示頁面時會是預設值。你可以在這個方法中把所選擇的選項的值傳遞給視圖,例如:

public ActionResult HDPharseEdit(string id)
{
    HDPharseViewModel model = GetHDPharseViewModel(id);
    HDPharseService hsrvc = new HDPharseService();
    model.OperatingOptions = GetOperatingOptions();
    model.SelectedOperatingOption = model.OPERATING; // 傳遞所選擇的選項
    return View("HDPharseEdit", model);
}

在你的 AddOrUpdate 方法中,你沒有將所選擇的選項的值設置到 hpharse 物件中,所以在存檔後沒有變化。你可以在這個方法中把所選擇的選項的值設置到 hpharse 物件中,例如:

hpharse = model.ToPharse(GetUserGuid());
hpharse.OPERATING = model.SelectedOperatingOption; // 設置所選擇的選項

我要發表回答

立即登入回答