ASP.NET MVC3提交将字典传递给Controller的帖子

时间:2012-08-30 14:48:40

标签: asp.net-mvc razor

我正在使用字典。当“提交”命中时,我如何将带有值的字典传递给[HttpPost]控制器方法?
目前在[HttpPost]方法中,DummyDictionary为空,我该如何解决这个问题?


谢谢!

MODEL

    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; }
        }
    }

CONTROLLER

    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);
            }

        }
    }

VIEW

    @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 =&gt; m.DummyDictionary)尝试将字典传递给post方法,但字典只是空的。如果没有这个@Html.HiddenFor,post方法中的Dictionary就是null。


非常感谢你的帮助,我很感激!


更新

TheObject

    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; }
        }
    }

1 个答案:

答案 0 :(得分:1)

它不漂亮但它有效。请记住包含TheObject的所有属性,如果不可见则隐藏,以便它们被发布。为此做一个帮助可能是一个好主意。

@model MvcApplication1.Models.TheModel

@using (Html.BeginForm())
{
    @Html.LabelFor(m => m.DummyDictionary["VT"].Name)
    @Html.Hidden("DummyDictionary[VT].Name",Model.DummyDictionary["VT"].Name)
    @Html.Hidden("DummyDictionary[VT].Token", Model.DummyDictionary["VT"].Token)
    @Html.TextBox("DummyDictionary[VT].Value", Model.DummyDictionary["VT"].Value)


    <input type="submit" class="submit_button" /><br />
}

控制器

    [HttpGet]
    public ActionResult Index()
    {
        TheModel m = new TheModel();
        m.Obj = new TheObject();
        TheObject a_obj = new TheObject();
        a_obj.Name = "Joe";
        m.DummyDictionary = new Dictionary<string, 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);
    }