Reputation: 1277
Noticed a problem, when I pass my model to the view, one of the properties of type int[] transforms into
<input type="hidden" value="System.Int32[]" name="test">
I'd expect to see stringified values of the array in the value property. I tried to use List. Which resulted into:
<input type="hidden" value="System.Collections.Generic.List`1[System.Int32]"
name="test">
I have quite complex Model, which depends on other classes, that is why I don't want to change it to string format. Is it not possible to use int[] ?
Upvotes: 0
Views: 475
Reputation: 9002
@Html.HiddenFor
accepts an object as the value. What you are seeing is the object.ToString()
, which for an Int32 array is the name of the type: "System.Int32[]".
If you want to simple store a csv version of your array in a hidden field, you could try the following:
@Html.Hidden("test", string.Join(",", values))
Upvotes: 0
Reputation: 31077
In the view you may need to do something like:
@Html.HiddenFor(x => x.Array, new { value = String.Join(",", Model.Array) })
Upvotes: 1
Reputation: 21485
Assuming that you would need to receive the values back to another action method via POST, you have to write your own code in the view that creates the HTML like this (one hidden field for each item in the list):
<input type="hidden" value="123" name="test[0]">
<input type="hidden" value="345" name="test[1]">
Read this for more information: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Upvotes: 1
Reputation: 44268
You're getting the .ToString()
value of the types which for more complex types is the type name.
Can you expose your collection properties in a different way. i.e.
public int [] MyIntegers
{
...
}
public string MyIntegersStringified
{
get {
return string.Join(",", MyIntegers);
}
}
Upvotes: 0