C# Visual Stadio 2017 core の MVCで
コントローラーからセッションのDTOに値をつめてVIEW側に渡したいのですが
下記のコードでは★のところで ディクショナリが空になっており値が取れないです。
コントローラー、VIEW間で値が受け渡せてVIEW側で取得できる方法を教えてください。
MVCは詳しくないです
実現したいことは
MVCモデルで コントローラーからセッションのDTOに値をつめてVIEW側に渡したい
質問したいこと
コントローラーからVIEW側にデータを渡した際に消えてしまうので
消えないで渡して、VIEW側できちんと値がとれる方法
制限
ViewData、ViewBag、TempData で渡してはいけないという制限があります
// Controller
public class TestController : Controller { public Int _inputIntA; public Int _inputIntB; public string _inputStrC = "xxx"; public IActionResult Index(SessionDTO model) { _inputIntA = 1; _inputIntB = 2; model.dicList.Add(nameof(_inputIntA), _inputIntA); model.dicList.Add(nameof(_inputIntB), _inputIntB); model.dicList.Add(nameof(_inputStrC), _inputStrC); return View(nameof(model), model); } }
// ModelsDTO
namespace TestProc.Models { public class SessionDTO { public Dictionary<string, object> dicList = new Dictionary<string, object>(); } }
// index.cshtml
@model TestProc.Models.SessionDTO @{ // ★ dicListが空になってしまう・・・ Model.dicList.TryGetValue("_inputIntA",out var _inputIntA); Model.dicList.TryGetValue("_inputIntB",out var _inputIntB); Model.dicList.TryGetValue("_inputStrC",out var _inputStrC); }
回答1件
あなたの回答
tips
プレビュー