就是吧! 當我要新增資料 發現 資料庫已經有這筆資料存在時,就改作Update的動作
Update這個Function 已經存在了,所以就想著直接CALL....
喔!!! 這件事就這麼發生了
介紹一下兩個Funtion
新增 -->
ActionResult AddNewProduct ,傳回 bool success, String ErroMsg
Update -->
ActionResult ProductUpdate 傳回 bool iResult , String Msg
傳回的東西都是
JsonResult , 我想要抓到 ProductUpdate 的Json 回到 AddNewProduct 傳出去
具體怎麼做剛開始還覺得有可能嗎?真的能成嗎?
一方面又覺得若不行 那真的太笨了 =__=
然後我就問了...行呀! 真的能~~
看看片段的實作 CODE
- [HttpPost]
- public ActionResult AddNewProduct(string ID)
- {
- bool iResult = true;
- string msg="";
- bool update = true;
- if (update)
- {
- //把要呼叫的Function ActionResult 轉型成 JsonResult
- JsonResult r = (JsonResult)ProductUpdate(ID,true);
-
- //因為拿到的r是object 所以再轉一次
- dynamic resultData = r.Data;
-
- //要用的時候,也要給他型別才行
- iResult = (bool)resultData.success;
- if(iResult)
- {
- msg = (String)resultData.FinishMsg;
- msg = msg.Replace("更新", "匯入");
- }else
- {
- msg = (String)resultData.ErrorMsg;
- msg = msg.Replace("更新", "匯入") + "請重新新增商品";
- }
- }
- else
- {
- //新增資料
- }
- return new JsonResult()
- {
- Data = new { success = iResult , Msg = msg }
- };
-
- }
附上
stackoverflow
還是GOOGLE的高亮程式碼漂亮阿!
其實每次要把CODE放上來我都會腦抽忘了怎麼弄ORZ