2017-10-19 40 views
0

新的孩子在塊瓦特/ ASP MVC ... tryna得到一個編輯表單進入JavaScript對話框。如何在使用Javascript調用Controller動作時使用ASP MVC中的Partial?

我的行動目前的計劃:

  • 主要觀點有編輯按鈕,抓住的onClick的RECORD_ID,使一個AJAX調用我的控制器,操作傳遞RECORD_ID作爲帕拉姆。
  • 在同一視圖中,我正在使用具有選項卡/對話框相關代碼的部分「_EditApp」。
  • 在同一個onClick中,我加載了我在_EditApp中指定的選項卡。

JS ..

$('.btn_edit_app').click(function() { 

    var app_to_edit = $(this).attr('id'); 
    $.ajax({ 
     url: '/Application/editApp', 
     contentType: 'application/html; charset=utf-8', 
     data: { app_id: app_to_edit}, 
     type: 'GET', 
     dataType: 'html', 
     success: function (result) {}, 
    }); 
    $('#edit_tabs').tabs({ active: 0 }); 
    $('#edit_dialog').dialog({ width: 700, height: 400 }); 
}); 

我的控制器/行動

public ActionResult editApp(int app_id) 
     {  
      AppDBServer ads = new AppDBServer(); 
      ads = findADS(app_id); 
      return View("_EditApplication", ads); 
     } 

問題...

簡單地說,我要檢索的記錄,並填充標籤和對話包含檢索的數據字段。因此將該模型傳遞給EditApplication Partial。

問題是我在我的主視圖中使用了相同的部分,我在控制器操作中,並不確定如何去做這個...想法,或者甚至是更新的方法都是A- OK。

此外,我的目標是由控制器/操作處理數據檢索。

謝謝你,SOF fam!

回答

0

我只想評論,但我不能,因爲我的聲望不到50。無論如何,如果我理解正確,你想在動作調用後拉出一些標籤。我以前不得不這樣做。這裏是我的解決方案:

onclick按鈕或行與ID調用ActionResult。

或者使用Ajax表格: @using(Ajax.BeginForm( 「GetTabs」, 「ControllerHere」,新AjaxOptions(){LoadingElementId = 「裝載」,UpdateTargetId = 「targetdiv」,InsertionMode = InsertionMode.Replace,列舉HTTPMethod = 「GET」})) { @ Html.Hidden( 「ID」,ID) .... 提交 }

返回一個PartialView即 「_Tabs」 並通過模型(ID在這種情況下)。

` 
public ActionResult GetTabs(string id) 
{ 
.... 
//pass id or get model and pass model 
return PartialView("_Tabs", id); 
} 
` 

在_Tabs.cshtml中爲每個選項卡調用Html.RenderAction。 (我使用bootstrap設置了標籤) Html.RenderAction採用一個操作方法名稱,您可以傳遞參數。

`@{Html.RenderAction("GetTab1",new {id = @id}) } 

@{Html.RenderAction("GetTab2",new {id = @id}) }` 

...等

每個Action將返回一個局部視圖... public ActionResult GetTab1(string id) { //get data model = id lookup return PartialView("_Tab1", model); } ...等

所以,現在我們有_Tabs局部視圖呈現其自己的部分視圖,每個可以有自己的模型。

當_Tabs部分完成渲染時,它會將HTML返回到主頁上的目標div,並在_Tabs.cshtml中創建x個標籤。

我也能夠保持與下面的腳本選擇的當前活動標籤:

<script> 
 
    $('#navtab a').click(function (e) { 
 
     e.preventDefault(); 
 
     $(this).tab('show'); 
 
    }); 
 

 
    // store the currently selected tab in the hash value 
 
    $("ul.nav-tabs > li > a").on("shown.bs.tab", function (e) { 
 
     var id = $(e.target).attr("href").substr(1); 
 
     window.location.hash = id; 
 
    }); 
 

 
    // on load of the page: switch to the currently selected tab 
 
    var hash = window.location.hash; 
 
    $('#navtab a[href="' + hash + '"]').tab('show'); 
 
</script>

相關問題