im really new to MVC and modelbinding so i guess im making something trival wrong, i try to bind some data;
View;
$.ajax({
url: '@Url.Action("Moo")',
type: "post",
dataType: "string",
contentType: "application/json",
data: JSON.stringify({"test": "bar", "test2": "bar2"}),
success: function (data) {}
});
Model;
Public Class MyTestData
Public Property test() As String
Public Property test2() As String
End Class
Controller;
Function Moo(test As MyTestData) As ActionResult
Return View()
End Function
When i run the dubugger i recive “nothing” from test in the controller, if i try with only one string the code works (the data in the view changed to {(test:bar)} and the controller datatype changed to from MytestData to String).
Any input is appriciated.
Rename your action parameter. It’s called
testand conflicting with thetestproperty that you have inside:I guess you got too many tests 🙂
FooBarseems like a better name 🙂Joke aside you also have a problem with the
dataTypeparameter. There’s no such value asstring. With ASP.NET MVC you usually do not need to supply it because the framework properly sets the HTTP Content-Type response header and jQuery will use it in order to parse the result automatically. So simply get rid of it, or make sure you have specified it to a correct value to match your actual return type. Since your action returns a view you could set it todataType: 'html'in order to be consistent.