简体   繁体   中英

Model binding not working for an array of int

This seemed pretty straight forward and I'm sure I have done it without issue in the past, but I have a model that has within it an array of int:

View model

public class ExampleViewModel
{
    public int ExampleProperty1 { get; set; }
    public int[] ExampleProperty2 = {};
    public string ExampleProperty3 { get; set; }
}

Now, I'll preload this view model with some data and show a form:

Controller action

public ViewResult Example1()
{
    var viewModel = new ExampleViewModel
    {
        ExampleProperty1 = 888,
        ExampleProperty2 = new [] { 1, 2, 3, 4 },
        ExampleProperty3 = "Test string"
    };
    return View(viewModel);
}

Razor view

@using (Html.BeginForm("example2", "class", FormMethod.Post, new {id = "ExampleForm"}))
{
    @Html.HiddenForEnumerable(m => m.ExampleProperty2)
    @Html.TextBoxFor(m => m.ExampleProperty1)
    @Html.TextBoxFor(m => m.ExampleProperty3)

    <input type="submit"/>
}

The "HiddenForEnumerable" extension method is shown below:

public static MvcHtmlString HiddenForEnumerable<TModel, TProperty>(this HtmlHelper<TModel> helper,
        Expression<Func<TModel, IEnumerable<TProperty>>> expression)
{
    var sb = new StringBuilder();

    var membername = expression.GetMemberName();
    var model = helper.ViewData.Model;
    var list = expression.Compile()(model).ToList();

    for (var i = 0; i < list.Count(); i++)
    {
        sb.Append(helper.Hidden(string.Format("{0}[{1}]", membername, i), list.ElementAt(i)));
    }

    return new MvcHtmlString(sb.ToString());
}

And generates the following HTML:

<input id="ExampleProperty2_0_" name="ExampleProperty2[0]" type="hidden" value="1">
<input id="ExampleProperty2_1_" name="ExampleProperty2[1]" type="hidden" value="2">
<input id="ExampleProperty2_2_" name="ExampleProperty2[2]" type="hidden" value="3">
<input id="ExampleProperty2_3_" name="ExampleProperty2[3]" type="hidden" value="4">

So far, so good. But when I submit the form to this action:

public ViewResult Example2(ExampleViewModel viewModel)
{
    return View();
}

The int array is empty:

空int数组

So the question is: How can I get MVC to correctly detect the int array and assign it to the view model?

Extra information

In case it's important, this is what the POST request body looks like:

ExampleProperty2%5B0%5D=1&ExampleProperty2%5B1%5D=2&ExampleProperty2%5B2%5D=3&ExampleProperty2%5B3%5D=4&ExampleProperty1=888&ExampleProperty3=Test+string

You need to make ExampleProperty2 a property (with getter/setter) so the DefaultModelBinder can set the value of the property

public class ExampleViewModel
{
  public int ExampleProperty1 { get; set; }
  public int[] ExampleProperty2 { get; set; } // change this
  public string ExampleProperty3 { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM