Erik Mandke
Erik Mandke

Reputation: 1667

ASP.net MVC posts int-array

Imagine this formular. (I edited the form, to show what I am trying to do.)

@using (Html.BeginForm("Add", "Notify", FormMethod.Post ))
{
   @foreach (KeyValuePair<Server, Services[]> server in ViewBag.ServerServices)
                        {
                            <div class="panel panel-default">
                                <div class="panel-heading" role="tab" id="[email protected]">
                                    <h4 class="panel-title">
                                        <a role="button" data-toggle="collapse" data-parent="#accordion" href="#@server.Key.ServerID" aria-expanded="false" aria-controls="@server.Key.ServerID">
                                            @server.Key.Servername
                                        </a>
                                    </h4>
                                </div>
                                <div id="@server.Key.ServerID" class="panel-collapse collapse" role="tabpanel" aria-labelledby="[email protected]">
                                    @foreach (Services service in server.Value)
                                    {
                                        <input class="form-control" type="checkbox" name="selectedServerServices[@server.Key.ServerID]" value="@service.ServiceID"/><label>@service.ServiceName</label>
                                        <br/>
                                    }
                                </div>
                            </div>
                        }

I would like to get the posted values and the keys too, but I always get a null object when trying to retrieve both.

In Request.AllKeys I can see that there are two values (e.g.) But the posted values do not get into the defined dictionary

My Action method looks like this.

    public ActionResult Add(Dictionary<string, int> test )

Upvotes: 1

Views: 689

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68675

I trying to solve your problem and find a way to do it.You only need to give names to your checkboxes's like this:

<input class="form-control" type="checkbox" name="test.selectedServerServices[@server.Key.ServerID]" value="@service.ServiceID"/><label>@service.ServiceName</label>

You must only add parameter_name.yourCustomName

This is my sample code:

<div> 
    @using(Html.BeginForm()){
        <input type="checkbox" name="checks.First" value="1" />
        <input type="checkbox" name="checks.Second" value="2" />
        <input type="checkbox" name="checks.Third" value="3" />
        <input type="checkbox" name="checks.Fourth" value="4" />
        <input type="submit" value="Submit"/>
    }
</div>

And this is my Action:

[HttpPost]
public ActionResult Index(Dictionary<string, int> checks)
{
       return View();
}

And this work like that you want.But be aware,if you will submit the form without selecting any checkbox,you will have two parameters in the dictionary: controller and action

Or you can use instead of that Custom Model Binder like this:

using System.Web.Mvc;

namespace YourNameSpace
{
    public class MyCustomModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.RequestContext.HttpContext.Request;

            Dictionary<string, int> test = new Dictionary<string, int>();

            test.Add("test[10]", int.Parse(request.Form["test[10]"]));
            test.Add("test[11]", int.Parse(request.Form["test[11]"]));

            return test;
        }
    }
}

And then register it in the Global.asax

protected void Application_Start()
{
   AreaRegistration.RegisterAllAreas();
   RouteConfig.RegisterRoutes(RouteTable.Routes);

   ModelBinders.Binders.Add(typeof(Dictionary<string,int>), new MyCustomModelBinder());
}

And finally use it in your method like this:

public ActionResult Add([ModelBinder(typeof(MyCustomModelBinder))]Dictionary<string, int> test )

Upvotes: 2

Related Questions