0

私は辞書を使っています。そして、「送信」がヒットしたときに、値を含む辞書を [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; }
        }
    }
4

1 に答える 1

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);
    }
于 2012-08-31T10:41:16.813 に答える