Daxxy Daxxy - 2 months ago 5x
ASP.NET (C#) Question

Post an Array of Models to Controller

I have an MVC5 project where I am trying to Post a List of 'Permissions' back to the server.

@model List<ConnectConsole.Models.Permissions>

<form action="/products/@{@Html.Raw(Url.RequestContext.RouteData.Values["id"])}/permissions/save" method="POST">
@for (int i = 0; i < Model.Count; i++)
@Html.TextBoxFor(model => model[i].ConnectId)

model => model[i].ScopeVal,
"-- Please Select --")
<button class="btn btn-full btn-submit">Save</button>
@* removed some html for brevity *@

The Data that is posted back to the server looks somthing like this (copied from chrome)


My C# model

public class Permissions
public String ConnectId { get; set; }
public List<SelectListItem> ScopeTypes;
public String ScopeVal;

Method signature that I am posting to

public ActionResult Save(string id, List<Permissions> permissions)

The object seems to be correctly posted to the server with all with all fields in the request but in the method the list that gets serialized from this data only contains the connect ID, it is of the correct size and I get a list that has 3 objects in it but ScopeVal is never set.

Is this a bug in MVC or am I doing something horribly wrong?


The MVC default Model Binder will only bind properties with public getters and setters.

You need to change the field

public String ScopeVal;

to be a property

public String ScopeVal { get; set; }

It's bad C# to expose class fields - this is what properties are for.