![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 ptw0518 ![]()
![]() |
public class ConfigurationType { public ConfigurationType() { Id = Guid.NewGuid().ToString(); } public string Id { get; set; } public string CodeValue { get; set; } } Controller public ActionResult TopMessage() { string type = "HP006"; List<Configuration> Configuration_C = APIMaster.CallGetAPI(new List<Configuration>(), "api/Configurations/ByType?type=" + type, out object errMsg); return View(Configuration_C); } View @model List<CustomerWeb.Models.Configuration> <div > <text>@Model[0].CodeValue</text> </div> 問題是這樣:我在控制器用別人寫的APi取資料,這段程式碼目前沒有問題,但是放到_Layout會出現@Model[0].CodeValue的錯誤訊息是 System.NullReferenceException: '並未將物件參考設定為物件的執行個體。' System.Web.Mvc.WebViewPage<TModel>.Model.get 傳回 null。 _Layout我只加入@Html.Partial("~/Views/Manage/TopMessage.cshtml") 請問該如何解決? |
1樓 |
好像只在該View正常運作,其他的View都失敗
本篇文章回覆於2020-08-13 10:38
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
2樓 |
好了我可以用了
在_Layout加入@Html.Action("_TopMessage", "Manage") 其中_TopMessage是部分檢視的View名稱,名稱要加底線_比較好 Manage是控制器方法名稱
本篇文章回覆於2020-08-13 11:10
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
3樓
最有價值解答
Allen ![]() ![]() |
你在view裡第一行有寫
@model List<CustomerWeb.Models.Configuration> 稍後在view裡寫 Model,指的就是它 你可以參考 https://stackoverflow.com/questions/4154407/asp-net-mvc-razor-pass-model-to-layout 在Controller裡將資料存在ViewData, 再在layout裡取得它
本篇文章回覆於2020-08-14 11:26
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
回覆 |
如要回應,請先登入. |