iT邦幫忙

0

如何使用@Html.Partial傳遞物件

如標題,以下敘述狀況--
View : Index.cshtml
PartialView : _TestReport.cshtml以及_Summary.cshtml
Index.cshtml得到物件Report1Report1這個物件裡面分別有ReportInformationSummary
請問要如何做到將Report1.ReportInformation傳給_TestReport.cshtml;將Report1.Summary傳給_Summary.cshtml
目前若使用ViewBag的方式是可行的,但記得可以直接在@Html.Partial("_TestReport", ) 逗號後面直接帶入傳入的東西,但網路上看到的都是使用model,請問若要使用dynamic的model要如何達到呢?

Index.cshtml

@using Multiverse.Data;
@using Newtonsoft.Json;

@{
    ViewBag.Title = "Index";

    var Report1 = Subject.FindInstances("Report").Cast<dynamic>().First(x => "A123"==(x.ReportInformation)?.ReportNumber);
        
    ViewBag.TestReport1 = Report1.ReportInformation;

}

<h2></h2>

@Html.Partial("_TestReport")                         
@Html.Partial("_Summary") 

_TestReport.cshtml

@using Multiverse.Data;
@using Newtonsoft.Json;

@{
   
    var row = ViewBag.TestReport;
}

<div> @(row.ReportNumber) </div>
fillano iT邦超人 1 級 ‧ 2019-08-16 16:12:09 檢舉
你在partial view裡面用@model宣告Model的型別,然後就可以用Model來取用傳進來的物件。
anniecat iT邦新手 3 級 ‧ 2019-08-16 16:36:18 檢舉
謝謝fillano,所以這個方式仍然需要額外的model才可以直接在View上做使用,對嗎?
anniecat iT邦新手 3 級 ‧ 2019-08-16 16:52:58 檢舉
還是說我可以使用dynamic的model去做呢? 但我目前還沒有找到相關做法...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
YoChen
iT邦研究生 1 級 ‧ 2019-08-16 15:59:55
最佳解答

你可試試看ViewData,但我覺得這樣跟用ViewBag沒太大差異~XDDD

@Html.Partial("_TestReport", Model, new ViewDataDictionary { { "TestReport1", Report1.ReportInformation } })

不過通常還是建議使用Model啦,
既然都已經做Partial了,
不就是為了放上對應的ViewModel嗎?
還是有什麼其他考量呢?

看更多先前的回應...收起先前的回應...
anniecat iT邦新手 3 級 ‧ 2019-08-16 16:35:24 檢舉

因為我們的資料庫不是關聯式資料庫,而且我的View的部分只是將資料撈出做呈現,所以可以直接從View下手。
謝謝你的幫忙,不過想嘗試看看能不能透過一個什麼方式直接從@Html.Partial(_partialView,)的,後面直接傳遞物件

YoChen iT邦研究生 1 級 ‧ 2019-08-16 17:09:45 檢舉

其實不管哪一種類型的DataBase,應該都不會影響到您將它轉為Model啦,您可以試著自己建看看~XDD
另外,您如果是要後面直接傳遞物件,不傳Model的話,您可以使用另外一個多載

@Html.Partial("_TestReport", new ViewDataDictionary { { "TestReport1", Report1.ReportInformation } })

然後在TestReport.cshtml的地方直接

ViewData["TestReport1"]

就可以取到值了

anniecat iT邦新手 3 級 ‧ 2019-08-16 17:22:42 檢舉

您提到的兩個做法,我都嘗試使用,謝謝~
另外,或許您會知道直接在View上宣告dynamic的model的作法嗎?

fillano iT邦超人 1 級 ‧ 2019-08-16 17:39:12 檢舉

google了一下:http://blogs.quovantis.com/dynamic-models-for-mvc-view/

不過我通常還是比較嚴格的用ViewModel。

YoChen iT邦研究生 1 級 ‧ 2019-08-16 17:41:15 檢舉

直接在TestReport.cshtml加入

@model dynamic

不過我自己沒有試過,
不行的話可能要Google看看~

YoChen iT邦研究生 1 級 ‧ 2019-08-16 17:44:20 檢舉

同意fillano大大的作法,自己簡單建一個ViewModel,方便有效~XDDD

anniecat iT邦新手 3 級 ‧ 2019-08-16 17:55:58 檢舉

謝謝fillano與YoChen的幫忙~我再多研究一下!

小魚 iT邦大師 1 級 ‧ 2019-08-16 20:04:24 檢舉

元件化

1
小魚
iT邦大師 1 級 ‧ 2019-08-16 20:09:31

我一般是從Controller傳ViewBag或Model到前端,Index. html和Partial都可以接收到

看更多先前的回應...收起先前的回應...
小魚 iT邦大師 1 級 ‧ 2019-08-16 20:10:12 檢舉

不喜歡的話也可以寫Ajax

anniecat iT邦新手 3 級 ‧ 2019-08-19 17:31:55 檢舉

謝謝小魚,目前實作的方式也是使用ViewBag,只是想嘗試看看使用dynamic的Model來實作看看~

anniecat iT邦新手 3 級 ‧ 2019-08-19 17:32:21 檢舉

請問你說的元件化是什麼呢?

小魚 iT邦大師 1 級 ‧ 2019-08-19 17:36:58 檢舉

元件化就是把畫面拆成幾個部分,
每個部分(Partial)就是一個元件,
可以直接放到很多畫面去使用,
像React等前端框架就會推薦使用元件化的方式.

anniecat iT邦新手 3 級 ‧ 2019-08-20 15:47:27 檢舉

瞭解~謝謝~

我要發表回答

立即登入回答