簡體   English   中英

asp.net mvc html.renderaction()將生成完整的HTML文檔

[英]asp.net mvc html.renderaction() will generate full HTML document

如果有人能回答我以下內容,我將不勝感激:

  • 我想在視圖中加入部分視圖。

  • 局部視圖提供的數據獨立於(主)視圖。

所以也許我有兩個控制器動作:

Public Class MyController

    Public Function GetMainView() As ActionResult
       Return View()
    End Function 'generates (main) view

    Public Function GetPartialView(ByVal myParameter As Integer) As ActionResult
       ViewBag.MyValue = "foo"
       If (myParameter = 101) Then
          ViewBag.MyValue = "bar"
       End If
       Return View()
    End Function 'generates partial view

End Class ' MyController

在主視圖中時,我可以寫

@Code
   Html.RenderAction("GetPartialView", New With {.myParameter = 101})
End Code

要么

Html.Action("GetPartialView", New With {.myParameter = 101})

問題是,這將在原始HTML文檔內生成一個完整的HTML文檔(帶有html和body標簽,以及所有這些內容……),除了有效代碼外,所有文檔均無效。 :-(

另一方面,如果我嘗試

@Code
   Html.RenderPartial("GetPartialView", New With {.myParameter = 101})
End Code

要么

Html.Partial("GetPartialView", New With {.myParameter = 101})

局部視圖正確呈現-但是忽略我傳遞的參數(甚至沒有輸入該方法...),因此ViewBag.MyValue將產生“ foo”。 :-(

除了通過AJAX動態加載局部視圖之外,是否還有其他方法可以克服此問題?

非常感謝!!!

您必須返回PartialView:

Public Class MyController

    Public Function GetMainView() As ActionResult
        Return View()
    End Function 'generates (main) view

    Public Function GetPartialView(ByVal myParameter As Integer) As ActionResult
        ViewBag.MyValue = "foo"
        If (myParameter = 101) Then
            ViewBag.MyValue = "bar"
        End If
        Return PartialView()  <<--
     End Function 'generates partial view

End Class ' MyController

您還可以顯式告訴局部視圖的名稱並傳遞其視圖模型,例如:

Return PartialView("partial view name", model)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM