2015年11月25日 星期三

[MVC]動態載入PartialView並傳回ViewModel


筆者設計操作界面時,習慣在同個頁面下作業,即在同個畫面上開窗。
如下圖右下角檢視按鈕按下後開啟視窗。(開窗畫面為PartialView)


如何實作?
步驟1:
    在同頁面上放上<div id="Panel"></div>

步驟2:
     透過AJAX取得該筆資料
function OpenSubMenu(id)
{
    $.ajax({
        url: '/Test/GetData/',
        type: 'post',
        data: { id: id },
        cache: false,
        async: false,
        success: function (res) {
                         $("#Panel").html(res); //將PartialView塞入Panel
        }
    });
}

步驟3:
 [HttpPost]
 public ActionResult GetData(int id)
{
    return PartialView("_PartailView", exchangeOutService.GetData(id));
 }

ps:exchangeOutService.GetData(id) 這段請自行實作資料層,筆者是透過服務層叫用資料

Visual Studio JS intellisense 失效解決方式

  試了好久,發現到工具>選項>IntelliCode js項目設定啟用,重新開啟VS就正常了! 後來發現是TypeScript3.2版有問題停用,使用4.3版的TypeScript即可