Description
I was able to use following code in asp.net mvc but it's not working in asp.net core and i can't see any other solution rather than writing a custom model binder. i tried whatever solution was out there on the internet but look like it's not working properly.
I am using asp.net core 2.0
ScoresViewModel.cs
public class ScoresViewModel
{
public List<List<int>> Inputs { get; set; }
}
index.cshtml
<from id="question" >
<input type="text" name="Inputs[0][0]" value="10" />
<input type="text" name="Inputs[0][1]" value="11" />
<button type="submit" id="submit" value="submit"/>
</from>
$("#submit").submit(function () {
var getData = function () {
return $("#question").serialize();
};
$.ajax({
url: "/Api/ScoreCalculator/Calculate",
method: "POST",
data: getData(),
success: function (response) {
$("#panel-report").html(response);
},
error: function (errResponse) {
debugger;
}
});
return false;
});
I can see in my browser that the request is post and mime type is x-www-form-urlencode
"postData": {
"mimeType": "application/x-www-form-urlencoded",
"text": "Inputs[0][0]=10&Inputs[0][0]=11",
"params": [
{
"name": "Inputs[0][0]",
"value": "10"
},
{
"name": "Inputs[0][1]",
"value": "11"
}
]
}
and in the controller i am getting the content
public class ScoreCalculatorController{
public IActionResult Calculate(ScoresViewModel scores){
return OK(scores)
}
}
i tried [FromBody]
and [FromForm]
but none is working, also if i just try to model bind a string it works fine so i believe i don't need to use [FromBody]
and [FromForm]
in here.
this code was working fine in mvc 5. look like asp.net core model binder is not supporting complex types correctly. any hint on this?
Expected behavvior
by passing Inputs[0][0]
the model binder, bind the value to a multi diemnsional array (int[][], list<int[]>, list<list>) or dictionary (dictionary<int, list>, dictionary<int, int[]> , ...) based on the type.
why it should works in this way?
@Html.TextBoxFor(m => m.Inputs[0][0])
is generating<input data-val="true" data-val-required="The Int32 field is required." id="Inputs_0__0_" name="Inputs[0][0]" type="text" value="1">
so passing theInputs[0][0]
should bind back to the same model- single dimensional array works the same way. simply passing something like
Inputs[0]
will do the binding topublic List<int> Inputs{get; set;}
- aps.net mvc works in this way
actual behavior
ignoring the data