How to bind Dictionary type parameter for both GET and POST action on ASP.NET MVC

假装没事ソ 提交于 2019-12-04 04:40:35

问题


I want to define a view which displays a list of label and checkbox, user can change the checkbox, then post back. I have problem posting back the dictionary. That is, The dictionary parameter for the post method is null.

Below are action method for both GET and POST action:

 public ActionResult MasterEdit(int id)
        {

            Dictionary<string, bool> kv = new Dictionary<string, bool>()
                                            {
                                                {"A", true},
                                                {"B", false}
                                            };

            return View(kv);
        }


        [HttpPost]
        public ActionResult MasterEdit(Dictionary<string, bool> kv)
        {
            return RedirectToAction("MasterEdit", new { id = 1 });
        }

Beliw is the view

@model System.Collections.Generic.Dictionary<string, bool>
@{
    ViewBag.Title = "Edit";
}
<h2>
    MasterEdit</h2>

@using (Html.BeginForm())
{ 

    <table>
        @foreach(var dic in Model)
        { 
            <tr>
                @dic.Key <input type="checkbox" name="kv" value="@dic.Value"  />
            </tr>


        }
    </table>


   <input type="submit" value="Save" />
}

Any idea would be very much appreciated!


回答1:


Don't use a dictionary for this. They don't play well with model binding. Could be a PITA.

A view model would be more appropriate:

public class MyViewModel
{
    public string Id { get; set; }
    public bool Checked { get; set; }
}

then a controller:

public class HomeController : Controller
{
    public ActionResult Index() 
    {
        var model = new[]
        {
            new MyViewModel { Id = "A", Checked = true },
            new MyViewModel { Id = "B", Checked = false },
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<MyViewModel> model)
    {
        return View(model);
    }
}

then a corresponding view (~/Views/Home/Index.cshtml):

@model IEnumerable<MyViewModel>

@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @Html.EditorForModel()
        </tbody>
    </table>
    <input type="submit" value="Save" />
}

and finally the corresponding editor template (~/Views/Home/EditorTemplates/MyViewModel.cshtml):

@model MyViewModel
<tr>
    <td>
        @Html.HiddenFor(x => x.Id)
        @Html.CheckBoxFor(x => x.Checked)
        @Html.DisplayFor(x => x.Id)
    </td>
</tr>



回答2:


Take a look at this post by scott hanselman. There're the examples of model binding to a dictionary, lists, etc



来源:https://stackoverflow.com/questions/6858061/how-to-bind-dictionary-type-parameter-for-both-get-and-post-action-on-asp-net-mv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!