私は辞書を使っています。そして、「送信」がヒットしたときに、値を含む辞書を [HttpPost] コントローラーメソッドに渡すにはどうすればよいですか?
現在 [HttpPost] メソッドで、DummyDictionary が空です。これを修正するにはどうすればよいですか?
ありがとう!
モデル
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DummyMVC.Objects;
namespace DummyMVC.Models
{
public class TheModel
{
public TheObject Obj { get; set; }
public Dictionary<string, TheObject> DummyDictionary { get; set; }
}
}
コントローラ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DummyMVC.Models;
using DummyMVC.Objects;
namespace DummyMVC.Controllers
{
public class DummyController : Controller
{
//
// GET: /Dummy/
[HttpGet]
public ActionResult Index()
{
TheModel m = new TheModel();
m.Obj = new Objects.TheObject();
TheObject a_obj = new TheObject();
a_obj.Name = "Joe";
m.DummyDictionary = new Dictionary<string, Objects.TheObject>();
m.DummyDictionary.Add("VT", a_obj);
return View(m);
}
[HttpPost]
public ActionResult Index(TheModel model)
{
// HERE THE DICTIONARY is EMPTY, where all the form values should exist.
string test = "";
return View(model);
}
}
}
見る
@model DummyMVC.Models.TheModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.DummyDictionary);
@Html.LabelFor(m => m.DummyDictionary["VT"].Name)
@Html.TextBoxFor(m => m.DummyDictionary["VT"].Value)<br />
@Html.TextBoxFor(m => m.DummyDictionary["VT"].Token, new { @Value = Model.DummyDictionary["VT"].Token, style = "display:none;" })
<input type="submit" class="submit_button" /><br />
}
ビューでは、 @Html.HiddenFor(m => m.DummyDictionary) を使用して辞書を post メソッドに渡そうとしていますが、辞書は空です。この @Html.HiddenFor がないと、post メソッドの Dictionary は null になります。
ご協力ありがとうございました。
アップデート
オブジェクト
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DummyMVC.Objects
{
public class TheObject
{
public string Name { get; set; }
public string Value { get; set; }
public string Token { get; set; }
}
}