2017-10-19 44 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>

相关问题