我有一本正在使用的字典。当“提交”被击中时,我如何将包含其中值的字典传递给 [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 方法中的字典为空。
非常感谢您的帮助,我很感激!
更新
物体
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; }
}
}